简体   繁体   English

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

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

How should I modify my request payload to add data as key value pair in a google docx using google docs API in python.我应该如何修改我的请求有效负载,以使用 python 中的谷歌文档 API 将数据作为键值对添加到谷歌文档中。

When I use the following payload, the alignment gets ruined.当我使用以下有效负载时,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: 在此处输入图像描述

How can I align it properly so that the output is something like如何正确对齐它,以便 output 类似于

Name Of the Organization        StackOverflow
Industry                        Software
Business_Id                     123

or can we put this in a table without showing the table borders?或者我们可以把它放在一个表格中而不显示表格边框吗?

In your situation, I would like to propose using a table for achieving your goal.在您的情况下,我想建议使用表格来实现您的目标。 When Docs API is used, the table can be created without borders.使用Docs API时,可以无边框创建表格。 But unfortunately, in the current stage, I had thought that it is difficult to directly create a table using Docs API.但遗憾的是,在现阶段,我曾认为使用Docs API很难直接创建表。 So I had created a library for managing the table on Google Document using Docs API.因此,我创建了一个库,用于使用 Docs API 管理 Google Document 上的表格。 In this answer, I would like to propose to achieve your goal using this library.在这个答案中,我想建议使用这个库来实现你的目标。

Usage:用法:

1. Install library. 1.安装库。

$ pip install gdoctableapppy

2. Sample script: 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. Testing. 3. 测试。

When the above script is run, the following result can be obtained.运行上述脚本时,可以得到如下结果。

在此处输入图像描述

References:参考:

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

相关问题 如何使用 Google API 检索 Google Docs 文档的内容 - How to retrieve contents of a Google Docs Document using the Google API Google Docs API和Python:如何更改文档的所有者? - Google Docs API and Python: How to change the owner of a document? 使用Google App Engine如何在谷歌文档中上传文档(python) - Using Google App Engine how to upload document in google docs (python) 如何使用 python 将文本附加到 Google Docs 文档的末尾? - How to append text to the end of a Google Docs Document using python? 如何使用google docs api更新google doc(Python) - How to update google doc using google docs api (Python) 在python中的值和键对中添加值列表 - Adding list of values in a value and key pair in python 如何使用谷歌文档 API 和 Python 将本地图像添加到谷歌文档? - How to add local image to google docs using Google docs API and Python? 如何在运行时使用python验证两个JSON API Response键值对匹配? - How to Validate two JSON API Response key-value pair matches on runtime using python? 我们如何使用谷歌文档 API 和 python 为谷歌文档中的页脚应用“不同的首页” - How can we apply "different first page" for footers in the google docs using google docs API and python 如果列表中没有键和值对,则使用python跳过此数据 - if there is not key and value pair in list then skip this data using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM