I am needing to filter out lines containing formatting like italic, bold,.... I used the following code to filter and print out those lines:
but when i save them to test.docx file they lose their original format:
what should i do if i want to print those lines and keep the formatting?
from docx import Document
def check_font(par):
flag = {
'bold': 0,
'italic':0,
'underline':0,
}
if par.bold:
flag['bold'] = 1
if par.italic:
flag['italic'] = 1
if par.underline:
flag['underline'] = 1
return flag
def repl(filename):
doc = Document(filename)
for p in doc.paragraphs:
for par in p.runs:
flag = check_font(par)
if flag['bold'] == 1:
p.bold = True
if flag['italic'] == 1:
p.italic = True
if flag['underline'] == 1:
p.underline = True
p.text = u" ".join(par.text)
doc.save('test.docx')
repl('tstt.docx')my input_file tstt.docx: Quote:This is example text:
- This is bold text
- I need change it to bold
- How way to do that
- This is italics text
but when i save them to test.docx file they lose their original format:
Quote:bold text
change it to bold
to do that
italics text
what should i do if i want to print those lines and keep the formatting?
