-1

i need to display all the fonts contained in the docx file. example: In my file has 3 lines of font : calibri , Times New Roman, Arial. I use this code but it only shows the first font it encounters:

import docx
doc = Document('path of file docx')
par = doc.paragraphs[0]
print(f"'{par.style.font.name}'")
print(f"'{par.runs[0].font.name}'")

1 Answers1

0

try to use for loop here:

use set() to capture unique fonts in optimal way


import docx
path = ''  # your docx file path
doc = docx.Document(path)
fonts = set()
for p in doc.paragraphs:
    print(p)
    name = par.runs[0].font.name
    fonts.add(name)
print(fonts)

Hari Prasad
  • 461
  • 1
  • 3
  • 9