Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for particular style using python-docx

from docx import *
document = Document('ABC.docx')

for paragraph in document.paragraphs:
 for run in paragraph.runs:
  if run.style == 'Strong':
   print run.text

This is the code I am using to open a docx file and to check if there is Bold text but I am not getting any result. If I remove the if statement , the entire file is printed without any formatting / styles. Can you please let me know how to identify text in particular style like Bold or Italics using python-docx ? Thank you

like image 889
Vijayaraghavan Ravi Avatar asked Dec 08 '22 05:12

Vijayaraghavan Ravi


1 Answers

Although bold and the style Strong appear the same when rendered, they use two different mechanisms. The first applies bold directly and the second applies a character style that can include any other number of font characteristics.

To identify all occurrences of text that appears bold, you may need to do both.

But to just find the text having bold applied you would do something like this:

for paragraph in document.paragraphs:
    for run in paragraph.runs:
        if run.bold:
            print run.text

Note there are ways this can miss text that appears bold, like text that appears in a paragraph whose font formatting is bold for the entire paragraph (Heading1 for example). But I think this is the property you were looking for.

like image 157
scanny Avatar answered Dec 15 '22 01:12

scanny