4

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!

NineBerry
  • 26,306
  • 3
  • 62
  • 93
Tyler Bell
  • 837
  • 10
  • 30

2 Answers2

5
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.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
4

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
Synthase
  • 5,849
  • 2
  • 12
  • 34