Hi, I am facing problem in formatting Docx file using python docx library.
I am able to replace some text in a paragraph but can't maintain text font.
from docx import Document
# read doc file
doc = Document(file name)
# now I need to replace few words in a paragraph,(lets say replace a key in the paragraph by val) and paragraph index(pid) is known so I am performing following tasks:
paragraph = doc.paragraphs[int(pid)].text
para = re.sub(key, val, paragraph.rstrip())
doc.paragraphs[pid].text = para
# After saving this document, now I can see replaced text in the file but not able to maintain the font. I mean earlier common font in the file was "Times New Roman" but after text replacement paragraph font and weight is different. So how can I maintain the paragraph font and other styling similar to before text replacement.
Or how to extract the existing font!!!