'How to underline text that is not in a specific font in a Word document using python-docx
I am working on some python (version 3.10.4) code on PyCharm (Community Edition 2021.3.3) using the python-docx library (version 0.8.1.1), that allows to determine if text in a Word document formatted in the 'Normal' style contains font that is Times New Roman. This functionality of the code (as shown below) works as required.
But I was wondering if it is possible to underline instances of text in the document where the font is not Times New Roman. I have tried to add some code that is supposed to underline text, but this does not work as required. Anyone have any ideas as to how this feature can be implemented in the code? Any form of help would be appreciated. If there are any questions about the code, please ask.
import docx # import the python-docx library
WordFile = docx.Document("state/the/file/directory") # Word document file directory for python-docx to access (use /)
# check font name for normal style text (body text/paragraphs) ********************
norm_font = set() # store all Normal style font names in the set norm_font
norm_misc_font = set() # store unacceptable Normal style font names in the set norm_misc_font
for paragraph in WordFile.paragraphs:
if 'Normal' == paragraph.style.name:
for run in paragraph.runs:
# check for duplicates and store in the set norm_font
if run.font.name not in norm_font:
norm_font.add(run.font.name)
# check if fonts are unacceptable, if so, store in the set norm_misc_font
if run.font.name != 'Times New Roman':
norm_misc_font.add(run.font.name)
# add underline to text not in Times New Roman
run = WordFile.add_paragraph().add_run()
run.underline = True
# check if all elements in norm_font are 'Times New Roman'
if {"Times New Roman"} == norm_font:
# print this if all elements in norm_font are 'Times New Roman'
print("All Normal text contain Times New Roman.")
# check if all elements in norm_font are not 'Times New Roman'
elif {"Times New Roman"} != norm_font:
# print this if all elements in norm_font are not 'Times New Roman' and print norm_misc_font content
print("Normal text contain unrecognised font(s):", norm_misc_font)
else:
print("Normal text font name operation failed.")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
