I am needing to filter out lines containing formatting like italic, bold,.... I used the following code to filter and print out those lines:
my input_file
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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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' ) |
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?