1

I have a python script which does some analysis and output the results as text (paragraphs) on a Google Doc. I know how to insert text, update paragraph and text style through batchUpdate.

doc_service.documents().batchUpdate(documentId=<ID>,body={'requests': <my_request>}).execute()

where, for instance, "my_request" takes the form of something like:

request = [
        {
            "insertText": {
                "location": {
                    "index": <index_position>,
                    "segmentId": <id>
                },
                "text": <text>
            }
        },
        {
            "updateParagraphStyle": {
                "paragraphStyle": {
                    "namedStyleType": <paragraph_type>
                },
                "range": {
                    "segmentId": <id>,
                    "startIndex": <index_position>,
                    "endIndex": <index_position>
                },
                "fields": "namedStyleType"
            }
        },
    ]

However, once the script is done updating the table, it would be fantastic if a table of content could be added at the top of the document.

However, I am very new to Google Docs API and I am not entirely sure how to do that. I know I should use "TableOfContents" as a StructuralElement. I also know this option currently does not update automatically after each modification brought to the document (this is why I would like to create it AFTER the document has finished updating and place it at the top of the document).

How to do this with python? I am unclear where to call "TableOfContents" in my request.

Thank you so very much!

2
  • 1
    The answer provided here will help you with the doubts you have Insert table in Google Docs API Python Commented May 12, 2020 at 9:38
  • Thank you! It does not quite answer my question though, as I am specifically looking to add a table of content, rather than just a table with text. I need a table that is looking through the document once it's finished, to record headings, a link to these headings, and page numbers. Commented May 12, 2020 at 10:53

1 Answer 1

2

After your comment, I was able to understand better what you are desiring to do, but I came across these two Issue Tracker's posts:

These are well-known feature requests that unfortunately haven't been implemented yet. You can hit the ☆ next to the issue number in the top left on this page as it lets Google know more people are encountering this and so it is more likely to be seen faster.

Therefore, it's not possible to insert/update a table of contents programmatically.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Alberto for your answer! Too bad this was not implemented yet, especially since it is relatively easy to manually add a table of content to a Google Doc. I will hit the star button according to what you mentioned. Again, it is much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.