Skip to main content
 首页 » 编程设计

python之在 Google Docs API Python 中插入表格

2024年11月24日9zhujiabin

来自 Working with tables  |  Google Docs API  |  Google Developers

requests = [{'insertTable': {"table": { 
"columns": 2, 
"rows": 2, 
"tableRows": [ 
    { "tableCells": [ 
            { 
                "content": [ { "paragraph": { ...  }, } ], 
            }, 
            { 
                "content": [ { "paragraph": { ... }, } ], 
            } 
        ], 
    }, 
    { 
        "tableCells": [ 
            { 
                "content": [ { "paragraph": { ... }, } ], 
            }, 
            { 
                "content": [ { "paragraph": { ... }, } ], 
            } 
        ], 
    } 
]}}}] 
result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute() 

我收到一个TypeError: Object of type set is not JSON serializable

请您参考如下方法:

  • 您想使用 Google 文档 API 在 Google 文档中插入表格。

如果我的理解是正确的,这个修改怎么样?用作请求主体的对象是 docs.documents.get 方法返回的对象。在这个回答中,我想向您展示 3 个示例。

示例脚本 1:

此示例脚本来自 the official document .

The following example inserts text into the first table cell of a table and adds a table row.

重要的一点是,在运行脚本之前,请创建新的 Google 文档并放置一个表格。然后,请使用脚本来创建文档。至此,Hello 的文本被放入表格的“A1”中,并向表格中添加一行。

脚本:

requests = [{ 
      'insertText': { 
        'location': { 
          'index': 5 
        }, 
        'text': 'Hello' 
    } 
  }, 
  { 
    'insertTableRow': { 
        'tableCellLocation': { 
            'tableStartLocation': { 
                    'index': 2 
            }, 
            'rowIndex': 1, 
            'columnIndex': 1 
        }, 
        'insertBelow': 'true' 
    } 
  } 
] 
 
result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute() 

示例脚本 2:

在此示例脚本中,创建了一个包含 2 行和 2 列的新表。

脚本:

requests = [ 
    { 
        "insertTable": 
        { 
            "rows": 2, 
            "columns": 2, 
            "location": 
            { 
                "index": 1 
            } 
        } 
    } 
] 
 
result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute() 

示例脚本 3:

很遗憾,虽然我一直在官方文档中寻找创建表格并将值放入每个单元格的方法,但我找不到。所以我对此进行了实验。在此示例脚本中,我向您展示了创建表格并将值放入每个单元格的方法。

此示例脚本的流程如下。

  1. 创建一个 2 行 2 列的表格。
  2. 将 A1、B1、A2 和 B2 的文本放入单元格“A1:B2”表的。
    • 根据我的实验,获得了以下结果。
      • 对于行,索引需要设置为每5个索引。
      • 对于column,索引需要设置每2个index。
      • 重要的一点是,将值放入单元格时,请按“B2”、“A2”、“B1”和“A1”的顺序排列。因为当“A1”第一次被放置时,其他单元格的索引被改变。

脚本:

requests = [ 
    { 
        "insertTable": 
        { 
            "rows": 2, 
            "columns": 2, 
            "location": 
            { 
                "index": 1 
            } 
        } 
    }, 
    { 
        "insertText": 
        { 
            "text": "B2", 
            "location": 
            { 
                "index": 12 
            } 
        } 
    }, 
    { 
        "insertText": 
        { 
            "text": "A2", 
            "location": 
            { 
                "index": 10 
            } 
        } 
    }, 
    { 
        "insertText": 
        { 
            "text": "B1", 
            "location": 
            { 
                "index": 7 
            } 
        } 
    }, 
    { 
        "insertText": 
        { 
            "text": "A1", 
            "location": 
            { 
                "index": 5 
            } 
        } 
    } 
] 
 
result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute() 

注意事项:

  • 这些示例脚本使用 https://www.googleapis.com/auth/documents 的范围。请注意这一点。
  • 在这些示例脚本中,假设您已经使用过 Google Docs API。如果运行脚本时出现错误,请检查the Quickstart .
  • Google Docs API 现在正在增长。所以我认为将来可能会添加针对这种情况的更简单的方法。

引用资料:

如果我误解了您的问题并且这不是您想要的结果,我深表歉意。