Reputation: 41
I am generating docx documents by using docxtpl and Jinja2. I produced it by using as explained in the manual a python matrix/dictionnary feeded by data from XML files. When XML files store latin-1 data my docx are well generated but when the contents are in utf-8 encoding (like polish language) Jinja2 or docxtpl do not keep this encoding and the docx is apparently not well formatted.
How to pass an encoding option or circle this issue?
My process is very simple:
doc = DocxTemplate(<my_jinja2_docx_template>)
doc.render(<my_dictionnary_with_data>)
doc.save(<my_docx_file>)
Thanks a lot
Upvotes: 4
Views: 788
Reputation: 3621
Take a look at the source code for the DocxTemplate
class at https://github.com/elapouya/python-docx-template/blob/master/docxtpl/template.py. There does not appear to be an option to change the encoding parameter. However, you may be able to add that functionality by creating your own child class and replacing the class's xml_to_string
method. That seems to be where the encoding is specified. The syntax for a child class would look like this:
class NewDocxTemplate(DocxTemplate):
def xml_to_string(self, xml):
return etree.tostring(xml, encoding=<your_encoding_here>, pretty_print=False)
Then instantiate NewDocxTemplate()
instead of DocxTemplate()
. All the methods from DocxTemplate()
will inherit except the one you changed.
Upvotes: 2