繁体   English   中英

使用谷歌文档 API 和 python 在键值对中添加数据时如何改善文档中的间距

[英]How to improve spacing in a document when adding data in key value pair using google docs API and python

我应该如何修改我的请求有效负载,以使用 python 中的谷歌文档 API 将数据作为键值对添加到谷歌文档中。

当我使用以下有效负载时,alignment 被破坏了。

requests = [{
   "insertText":{
      "text":"\nName of the Organization\t\t\t\tStackOverflow\nIndustry\t\t\t\tSo
ftware\nBusiness_Id\t\t\t123\n",
      "location":{
         "index":4
      }
   }
}]

Output: 在此处输入图像描述

如何正确对齐它,以便 output 类似于

Name Of the Organization        StackOverflow
Industry                        Software
Business_Id                     123

或者我们可以把它放在一个表格中而不显示表格边框吗?

在您的情况下,我想建议使用表格来实现您的目标。 使用Docs API时,可以无边框创建表格。 但遗憾的是,在现阶段,我曾认为使用Docs API很难直接创建表。 因此,我创建了一个库,用于使用 Docs API 管理 Google Document 上的表格。 在这个答案中,我想建议使用这个库来实现你的目标。

用法:

1.安装库。

$ pip install gdoctableapppy

2.示例脚本:

docs = build('docs', 'v1', credentials=creds) # Please use your script here.
documentId = "###" # Please set Google Document ID.

values = [['Name Of the Organization', 'StackOverflow'], ['Industry', 'Software'], ['Business_Id', '123']]
resource = {
    "oauth2": creds,
    "documentId": documentId,
    "rows": len(values),
    "columns": len(values[0]),
    "append": True,
    "values": values,
}
gdoctableapp.CreateTable(resource)
resource = {
    "oauth2": creds,
    "documentId": documentId,
}
res = gdoctableapp.GetTables(resource)
obj = {"color": {"color": {}}, "dashStyle": "SOLID", "width": {"magnitude": 0, "unit": "PT"}}
requests = [{
    "updateTableCellStyle": {
        "tableCellStyle": {
            "borderBottom": obj,
            "borderTop": obj,
            "borderLeft": obj,
            "borderRight": obj,
        },
        "tableStartLocation": {
            "index": res['tables'][-1]['tablePosition']['startIndex']
        },
        "fields": "borderBottom,borderTop,borderLeft,borderRight"
    }
}]
docs.documents().batchUpdate(documentId=documentId, body={'requests': requests}).execute()

3. 测试。

运行上述脚本时,可以得到如下结果。

在此处输入图像描述

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM