paragraph font extraction using pydocx

4 views
Skip to first unread message

M S Pangtey

unread,
Jul 14, 2015, 8:21:42 AM7/14/15
to web...@googlegroups.com
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!!!

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages