Search code examples
pythongoogle-docsgoogle-docs-api

How to append text in end of document and apply style to it


I'm trying to append a heading and paragraph into an existing google docs document, I'm able to insert text at the end of document using:

{
  'insertText': {
      'endOfSegmentLocation': {},
      'text': '\n' + HEADING + '\n'
  }
}

But when we want to apply styling with insert text we need a start_index and end_index, the problem is I don't have start_index for this as I'm appending it to the end of document.

Complete code:

requests = [
            {
                'insertText': {
                    'endOfSegmentLocation': {},
                    'text': '\n' + HEADING + '\n'
                }
            },
        {
            'updateParagraphStyle': {
                'range': {
                    'startIndex': HOW TO FIND START INDEX?,
                    'endIndex':  len(HEADING)
                },
                'paragraphStyle': {
                    'namedStyleType': 'HEADING_1',
                    'spaceAbove': {
                        'magnitude': 10.0,
                        'unit': 'PT'
                    },
                    'spaceBelow': {
                        'magnitude': 10.0,
                        'unit': 'PT'
                    }
                },
                'fields': 'namedStyleType,spaceAbove,spaceBelow'
            }
        },
        {
            'insertText': {
                'endOfSegmentLocation': {},
                'text': PARAGRAPHS + '\n'
            }
        },
    ]

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

Solution

  • In the current stage, unfortunately, the property of range has no endOfSegmentLocation and both startIndex and endIndex are required to be used. So, in this case, first, the last index is required to be retrieved. When this is reflected in your showing script, it becomes as follows.

    Modified script:

    res1 = service.documents().get(documentId=DOCUMENT_ID).execute() # Here, last index is retrieved.
    startIndex = res1["body"]["content"][-1]["endIndex"]
    requests = [
        {"insertText": {"endOfSegmentLocation": {}, "text": "\n" + HEADING + "\n"}},
        {
            "updateParagraphStyle": {
                "range": {
                    "startIndex": startIndex,
                    "endIndex": startIndex + len(HEADING),
                },
                "paragraphStyle": {
                    "namedStyleType": "HEADING_1",
                    "spaceAbove": {"magnitude": 10.0, "unit": "PT"},
                    "spaceBelow": {"magnitude": 10.0, "unit": "PT"},
                },
                "fields": "namedStyleType,spaceAbove,spaceBelow",
            }
        },
        {"insertText": {"endOfSegmentLocation": {}, "text": PARAGRAPHS + "\n"}},
    ]
    result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={"requests": requests}).execute()
    

    References: