Open pecanka opened 3 weeks ago
Could you provide the 2 .docx template files (Example A & B)
certainly. and I'll do one better and provide all 4 of the cases I was mentioning above. they are the following:
document1.docx # tags between <w:tcPr> and </w:tcPr> => works well
document2.docx # newline between <w:tcPr> and </w:tcPr> => works well
document3.docx # nothing between <w:tcPr> and </w:tcPr> => fails
document4.docx # shorthand tag <w:tcPr/> => fails
and here's the code to run the conversions:
def run_docxtpl(file):
import jinja2
from docxtpl import DocxTemplate
doc = DocxTemplate(file)
doc.render({}, jinja2.Environment(), autoescape = True)
doc.save(f'{file}.docx')
run_docxtpl("document1.docx")
run_docxtpl("document2.docx")
run_docxtpl("document3.docx")
run_docxtpl("document4.docx")
document1.docx document2.docx document3.docx document4.docx
and here are the output files I get when I run the above code using docxtpl 0.18.0 on Python 3.10.11
document1.docx.docx document2.docx.docx document3.docx.docx document4.docx.docx
hope this helps
Description of issue
Some writers (e.g., Pandoc) produce valid docx files with shorthand tags such as
<w:tcPr />
instead of the fullhand<w:tcPr></w:tcPr>
. This causes the functionality of docxtpl inside its methodpatch_xml()
such as the setting of cell background color via{% cellbg <var> %}
to not function properly.Below are two examples of document.xml that differ in having
<w:tcPr>\n</w:tcPr>
(works) and<w:tcPr />
(does not work). Both documents contain the tag{% cellbg 444444 %}
. In the former case, the application of docxtpl works. In the latter case it does not.OK (before)
OK (after)
Fail (before)
or
Fail (after)
New lines
As shown above, a plain pair
<w:tcPr></w:tcPr>
(i.e., without at least the new line between the tags), which Word normalizes to<w:tcPr />
anyway, will not work either. In a real Word document, i.e., without new line symbols, a valid example of "OK before" would be this:<w:tcPr><w:tcW w:w="0" w:type="auto"/></w:tcPr>
This is also use in Example A below.
Severity
None of this breaks the document to the degree that Word would fail to open, but in the latter case there is no cell background color after Word removes the line
<w:shd w:val="clear" w:color="auto" w:fill="444444 "/>
.Reproduceable example
Example A: Content of document.xml where docxtpl works
Example B: Content of document.xml where docxtpl DOES not work