html2docx icon indicating copy to clipboard operation
html2docx copied to clipboard

Create document from Word template

Open rickmedlin opened this issue 3 years ago • 2 comments

  • Can it create a document by making a copy of an existing template? I've tried this without success:

    document = Document('template.docx') new_parser = HtmlToDocx()

    html = sys.argv[1] new_parser.add_html_to_document(html, document)

    filename = sys.argv[2] + '.docx' document.save(filename)

    print(filename) return filename

  • If the Word template/new document has bookmarks, can I insert text at the bookmarks?

rickmedlin avatar Oct 22 '21 20:10 rickmedlin

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

pqzx avatar Oct 24 '21 05:10 pqzx

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)

djplaner avatar Jan 11 '22 20:01 djplaner