簡體   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