Bhautik
Bhautik

Reputation: 35

How to get actual style of text in word document using python docx

I am using python docx library to read MS word file(.docx). When i read paragraph i use font function to get all style properties. But sometimes it gives None for font size attribute. Is there any way to get actual font size which paragraph contains. Example code is given below which i am using to parse paragraphs

from docx import Document
d = Document(document_path)
for paragraph in d.paragraphs:
    for run in paragraph.runs:
        print (run.font.size)

Upvotes: 2

Views: 3609

Answers (2)

scanny
scanny

Reputation: 28863

Short answer is no. What you're asking for is effective font size and python-docx can only see an explicitly set font size. When font.size reports None, it is the default for that paragraph, whatever that is, which depends on the style hierarchy.

In many cases it might be the font size of the applicable paragraph style, but the only way to know for sure is to traverse the style hierarchy for that text node to the first explicit definition.

Upvotes: 1

Vaibhav Jadhav
Vaibhav Jadhav

Reputation: 2076

The following code worked for me:

Divide it by 12700 to get actually font size.

import docx
docFile = docx.Document("C:/Users/vjadhav6/Desktop/testFile.docx")
for i in docFile.paragraphs:
    for j in i.runs:
        print(j.font.size/12700)

Upvotes: 2

Related Questions