Obtaining font attributes of Table of Contents, List of Figures, List of Tables

Viewed 16

I am attempting to acquire the font attributes (font, font size, font colour) of text in the Table of Contents, List of Figures and List of Tables in a Word document by using python-docx (version 0.8.11)

The text in such areas are found using the 'Hyperlink' style as shown in the code snippet. This text can also be printed.

However I am unable to discover its font attributes. I tried to find the font by using i.font, but I get the error: AttributeError: 'CT_R' object has no attribute 'font'. Not sure what this means.

I would like to know if it is possible to obtain the font attributes of text in the Table of Contents, List of Figures and List of Tables in a Word document. Any help would be appreciated. The code is shown below.

import docx  # python-docx library

# open docx file
document = docx.Document("file/path")

elements = document._body._body
rs = elements.xpath('.//w:r')
table_of_contents = [r for r in rs if r.style == "Hyperlink"]
for i in table_of_contents:
    print(i.text)  # print all text
    print(i.font)
0 Answers
Related