0
votes

Currently trying to create a python script that will check a google document for various SEO onpage metrics. To perform my checks I need to be able to split out the H1, H2-H4, text in bold etc & this is a follow-up question to my previous request for help on splitting out the various headings.

The following is how I've been pulling the headings:

def read_paragraph_element(element):
    text_run = element.get('textRun')
    if not text_run:
        return ''
    return text_run.get('content')


def read_structural_elements(elements):    
    for value in elements:
        if 'paragraph' in value and value['paragraph']['paragraphStyle']['namedStyleType'] == 'HEADING_2':
            elements = value.get('paragraph').get('elements')
            for elem in elements:
                sub_headings += read_paragraph_element(elem)

When I try to follow a similar logic for bold or italic text, I end up with a blank list:

    for value in elements:
        if 'paragraph' in value and value['paragraph']['paragraphStyle']['namedStyleType']['paragraphElement']['textStyle'] == 'bold':
            elements = value.get('paragraph').get('elements')
            for elem in elements:
                sub_headings += read_paragraph_element(elem)

I'm pretty new to coding/python and am struggling to see where to go from here.

1

1 Answers

0
votes

If you print the content of value in your code, it will show something like this:

{
   "startIndex":1,
   "endIndex":28,
   "paragraph":{
      "elements":[
         {
            "startIndex":1,
            "endIndex":16,
            "textRun":{
               "content":"THIS IS JUST A ",
               "textStyle":{
                  
               }
            }
         },
         {
            "startIndex":16,
            "endIndex":21,
            "textRun":{
               "content":"TEST ",
               "textStyle":{
                  "bold":true
               }
            }
         },
         {
            "startIndex":21,
            "endIndex":27,
            "textRun":{
               "content":"STRING",
               "textStyle":{
                  "bold":true,
                  "italic":true
               }
            }
         },
         {
            "startIndex":27,
            "endIndex":28,
            "textRun":{
               "content":"\n",
               "textStyle":{
                  "italic":true
               }
            }
         }
      ],
      "paragraphStyle":{
         "namedStyleType":"NORMAL_TEXT",
         "direction":"LEFT_TO_RIGHT"
      }
   }
}

To determine if the text are in bold or italic format, you should check the textStyle of each element inside the elements object.

Modified script:

From:

def read_paragraph_element(element):
    text_run = element.get('textRun')
    if not text_run:
        return ''
    return text_run.get('content')

To:

def read_paragraph_element(element):
    text_run = element.get('textRun')
    if not text_run:
        return ''
    else:
        style = text_run.get('textStyle')
        if style.get('bold') or style.get('italic'):
            return text_run.get('content')
        return ''

Note: The script above will only grab the text that are in bold or italic format.

Example:

Docs:

enter image description here

Output:

enter image description here