简体   繁体   中英

How to update google doc using google docs api (Python)

I'm new to Google Docs api and want to be able to add text using the replaceText option. How would I set this up? I am doing this in Python 3.6

Just follow the steps from Google Docs API quickstart using Python. Then try to run this code to insert text using InsertTextRequest method:

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()

To insert text into a document, use the BatchUpdate method and include an InsertTextRequest with the text and location as the payload. It's better to use this suggested method in the documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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