elapouya / python-docx-template

Use a docx as a jinja2 template
GNU Lesser General Public License v2.1
1.91k stars 378 forks source link

tpl.get_undeclared_variables() resets rendered template #465

Closed Slarag closed 1 year ago

Slarag commented 1 year ago

Describe the bug

A call to tpl.get_undeclared_variables() resets document rendering and returns all variables defined in the template. This conflicts with the documentation

To Reproduce

Run the code below on any template with some jinja variables.

import os.path
from docxtpl import DocxTemplate

doctemplate = r'test.docx'

if __name__ == '__main__':
    doc = DocxTemplate(os.path.abspath(doctemplate))
    context = {
        'test': 'HelloWorld'
    }
    doc.render(context)

    # Without the line below, rendering works fine. With this line, the rendering will be reset
    print(doc.get_undeclared_template_variables())

    doc.save("output.docx")

Expected behavior

tpl.get_undeclared_variables() must not reset the rendered template.

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context

To fix this issue, change line 47 of template.py from if not self.docx or self.is_rendered: to if not self.docx or not self.is_rendered:

test.docx

Slarag commented 1 year ago

Issue was introduced with commit 23038b98de1f5d8cc710145ceda596a61816583a

elapouya commented 1 year ago

fixed in v0.16.5