Search code examples
pythonhtmlpython-docxitalics

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!


Solution

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