Skip to content

Unintended changes to tags in tables #663

Description

@nathancoliver

I assume this should be categorized as a help wanted issue as opposed to a bug, given how many examples seem to work correctly for other people. I suspect there is a formatting issue within my docx template, but I want to at least mention these issues and hopefully someone will be able to help me.

I am using tags to dynamically add rows to a table, and depending on the template that I use, I get different error messages. I used the debugger to extract the xml within docxtpl to glean more into what is going on under the hood.

I have used different templates, with different error messages, and I will detail them below.

Maximum Broken Code

I won't share this example, but this is where I started going down the rabbit hole and is worth mentioning. Using the debugger within docxtpl, it appears the tags below have changed within docxtpl to the following:

{%tr for item in items %} -> {% tr for item in items %}
{%tr endfor %} -> {% tr endfor %}

As you can see, white space is added between % and tr. I also used docxtpl-checker to diagnose the issue, and the validation report shows the same issue.

The error message for this example is as follows:

Traceback (most recent call last):
  File "<my_file.py>", line 34, in <module>
    template.render(context)
    ~~~~~~~~~~~~~~~^^^^^^^^^
  File "<my_file.py>", line 490, in render
    xml_src = self.build_xml(context, jinja_env)
  File "<my_file.py>", line 437, in build_xml
    xml = self.render_xml_part(xml, self.docx._part, context, jinja_env)
  File "<my_file.py>", line 322, in render_xml_part
    raise exc
  File "<my_file.py>", line 312, in render_xml_part
    template = Template(src_xml)
  File "jinja2\environment.py", line 1214, in __new__
    return env.from_string(source, template_class=cls)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "jinja2\environment.py", line 1111, in from_string
    return cls.from_code(self, self.compile(source), gs, None)
                               ~~~~~~~~~~~~^^^^^^^^
  File "jinja2\environment.py", line 771, in compile
    self.handle_exception(source=source_hint)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "jinja2\environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<unknown>", line 551, in template
jinja2.exceptions.TemplateSyntaxError: tag name expected

Minimum Working Example

I created an MWE, with the table and code below:

Image
from pathlib import Path

from docxtpl import DocxTemplate

template_path = Path("build/template.docx")
save_path = Path("build/result.docx")

template = DocxTemplate(template_path)

context = {
    "items": [
        {
            "number": "One",
            "fruit": "Apple",
            "bird": "Falcon",
        },
        {
            "number": "Two",
            "fruit": "Orange",
            "bird": "Eagle",
        },
        {
            "number": "Three",
            "fruit": "Pear",
            "bird": "Cardinal",
        },
    ]
}

template.render(context)
template.save(save_path)

The error message and xml in this example are slightly different than in the first example. Using the debugger, the tags within docxtpl appear to be changed to the following:

{%tr for item in items %} -> {% for item in items %}
{%tr endfor %} -> {% endfor %}

This shows that the tr is removed from the tags above. docxtpl-checker tells a different story, which is that the tags are changed so that white space is added between % and tr (same as in the first example).

Below is the error message for the MWE:

Traceback (most recent call last):
  File "<my_file.py>", line 34, in <module>
    template.render(context)
    ~~~~~~~~~~~~~~~^^^^^^^^^
  File "<my_file.py>", line 490, in render
    xml_src = self.build_xml(context, jinja_env)
  File "<my_file.py>", line 437, in build_xml
    xml = self.render_xml_part(xml, self.docx._part, context, jinja_env)
  File "<my_file.py>", line 322, in render_xml_part
    raise exc
  File "<my_file.py>", line 312, in render_xml_part
    template = Template(src_xml)
  File "jinja2\environment.py", line 1214, in __new__
    return env.from_string(source, template_class=cls)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "jinja2\environment.py", line 1111, in from_string
    return cls.from_code(self, self.compile(source), gs, None)
                               ~~~~~~~~~~~~^^^^^^^^
  File "jinja2\environment.py", line 771, in compile
    self.handle_exception(source=source_hint)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "jinja2\environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<unknown>", line 2, in template
jinja2.exceptions.TemplateSyntaxError: unexpected '%'

Hopefully this is thorough enough to describe the issue I am encountering, and hopefully there is a simple solution to my predicament. Thanks a lot in advance.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions