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

Create a Docx template with both tags and values #481

Closed mire22 closed 1 year ago

mire22 commented 1 year ago

Describe your problem

Hi,

I would like to create a template with both tags and values in a docx document. The idea is that the user can after copy/paste the tags in his own document (an elaborated document, for example a report) and have at the same time the example of what will be the values after rendering. Basically, I want to have in the same docx escaped tags and tags. It's working well with {% raw %} and {% endraw %} for the "simple tags" but not for the "special syntax tags" (refering to doc) for the tables for example. The lines {%tr for item in table_contents %} and {%tr endfor %} are disapearing. I already tried the options of issue #355 but it's still not working.

Thank you for your help!

Provide a test case

automated_report_template_2.docx automated_report_2.txt: rename extension to .py

I'm expecting to find in the rendered document the elements between {% raw %} and {% endraw %} not rendered.

elapouya commented 1 year ago

did you try {%p raw %} ... {%p endraw %} (with a 'p') ? you may have also to escape {% and {{ with {_% and {_{ (see doc)

mire22 commented 1 year ago

Thank you! It works well with {_%tr for item in table_contents %_} and {_%tr endfor %_}. I tested {_ before but I wasn't using it well.

nonprofittechy commented 1 year ago

I've noticed you have to be very careful that the escaped tags are in the same "run". So editing an existing tag and inserting a may not work. I had to re-type the `{{and{_%` to be sure the escaped tags are properly parsed.