Tyler Bell
Tyler Bell

Reputation: 897

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!

Upvotes: 4

Views: 4636

Answers (2)

Synthaze
Synthaze

Reputation: 6090

Alternatively, from your html document:

from htmldocx import HtmlToDocx

new_parser = HtmlToDocx()
new_parser.parse_html_file("html_filename", "docx_filename")
#Files extensions not needed, but tolerated

Upvotes: 5

NineBerry
NineBerry

Reputation: 28499

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.

Upvotes: 5

Related Questions