Reputation: 11
How can I copy the content of an HTML file (data_1.html") to a specific location in a pre-made "template.docx" and then save it as "output.docx" with python?
I know, that it is possible with this simple Code to convert html to docx perfectly included formats like (bold,underline,italic):
from htmldocx import HtmlToDocx
new_parser = HtmlToDocx()
new_parser.parse_html_file(input_html_file_path, output_docx_file_path)
But somehow I'm not able to replace a word/tag with the content, like it is kind of possible with "python-docx-template". There it is easy to define a word, that replaces specific text within a template like here:
from docxtpl import DocxTemplate
doc = DocxTemplate("my_word_template.docx")
context = { 'company_name' : "World company" }
doc.render(context)
doc.save("generated_doc.docx")
But how is it possible to define the content of a HTML-file that replaces the word in a "template.docx" file?
When I try it like this...:
from docxtpl import DocxTemplate
doc = DocxTemplate("template.docx")
data1 = open("data_1.html", "r")
context = { 'MONTAG' : data1 }
doc.render(context)
doc.save("output.docx")
...and try to open the "output.docx", I get an error like "Error opening file in Word"
Here is an example of how I would like it to work:
Upvotes: 1
Views: 512