Skip to content Skip to sidebar Skip to footer

How To Pull Headings From Google Document Using API

Currently trying to create a python script that will check a google document for various SEO onpage metrics. The google docs API has a good sample showing how to extract ALL the te

Solution 1:

I believe your goal and your current situation as follows.

  • You want to retrieve the texts of HEADING_2 of the paragraph style.
  • You want to achieve this using googleapis for python.
  • You want to achieve your goal using the script in your question.
  • You have already been get the values from Google Document using Docs API.

Modification point:

  • In this case, I thought that when the value of namedStyleType is HEADING_2, the text is required to be retrieved.

When this point is reflected to your script, it becomes as follows.

Modified script:

From:
for value in elements:
    if 'paragraph' in value:
        elements = value.get('paragraph').get('elements')
To:
for value in elements:
    if 'paragraph' in value and value['paragraph']['paragraphStyle']['namedStyleType'] == 'HEADING_2':  # Modified
        elements = value.get('paragraph').get('elements')

Reference:


Post a Comment for "How To Pull Headings From Google Document Using API"