如何使用Google文档API(Python)更新Google文档

时间:2019-03-27 07:09:46

标签: python json google-docs-api

我是Google文档api的新手,并且希望能够使用replaceText选项添加文本。我该如何设置?我正在Python 3.6中做到这一点

1 个答案:

答案 0 :(得分:0)

只需使用Python遵循Google Docs API quickstart中的步骤即可。然后尝试运行此代码以使用InsertTextRequest方法插入文本:

requests = [
     {
        'insertText': {
            'location': {
                'index': 25,
            },
            'text': text1
        }
    },
             {
        'insertText': {
            'location': {
                'index': 50,
            },
            'text': text2
        }
    },
             {
        'insertText': {
            'location': {
                'index': 75,
            },
            'text': text3
        }
    },
]

result = service.documents().batchUpdate(
    documentId=DOCUMENT_ID, body={'requests': requests}).execute()

要将文本插入文档,请使用BatchUpdate方法,并包括一个InsertTextRequest,并将文本和位置作为有效内容。最好在文档中使用这种建议的方法。