pqzx / html2docx

Convert html to docx
MIT License
69 stars 49 forks source link

Create document from Word template #25

Open rickmedlin opened 2 years ago

rickmedlin commented 2 years ago
pqzx commented 2 years ago

Hi @rickmedlin, I'm not sure I understand what you mean. What were you trying to do when you wrote that code, and what did it end up doing?

For the second question: this package isn't designed to handle that, but since it's built on top of python-docx, you may be able to directly use the methods on the Document object to get the behaviour you want. Have a look at https://python-docx.readthedocs.io/en/latest/index.html

djplaner commented 2 years ago

Hi @rickmedlin and @pqzx, FWIW, I've been successfully using python-docx to open a template document and add in converted HTML. The trick is to pass a path to the template Word doc when calling Document() e.g. the following from my code (the template document is definined using settings)

Sorry, don't know anything about using bookmarks in the template document.

However, my vague recollection is that if the template document already has content, add_html_document will add the converted HTML content at the end of that content.

template = settings.CONTENT_INTERFACE_DOC_TEMPLATE
document = Document(template)

new_parser = HtmlToDocx()
new_parser.add_html_to_document(html,document)    
document.save(filename)