Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-Docx Inserting HTML into Docx

Is it possible to insert HTML into a Document using python-docx with styling applied? The only thing I need to work are italics.

For example how to insert "Today is <i>Saturday</i>." with Saturday actually being inserted with italics?

Thanks!

like image 992
Tyler Bell Avatar asked Oct 15 '25 17:10

Tyler Bell


1 Answers

p = document.add_paragraph()
p.add_run('Today is ') 
p.add_run('Saturday').italic = True 
p.add_run('.') 

The library doesn't understand html. You have to parse text yourself, separating italic text from non-italic text and add it to the document as shown above.

like image 126
NineBerry Avatar answered Oct 18 '25 12:10

NineBerry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!