elapouya / python-docx-template

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

Numbering breaking when deep nesting subdocuments #500

Open rgrignon1 opened 1 year ago

rgrignon1 commented 1 year ago

Describe the bug

In a project with multiple layers of subdocuments, when inserting a subdocument containing a numbered list, pushing it a level deeper break the continuation

To Reproduce

Start with a first docx template with a numbered list generated by a loop (using explicit Word numbering list):

{% for item in content %}
1. {{ item }}{% endfor %}

then follow up with a second docx template:

{{ FIRST_TEMPLATE }}

And then a third:

{{ SECOND_TEMPLATE }}

Now, here comes the generation of the templates into new documents:

# source
tpl_1_path = "./path/first_template.docx"
tpl_2_path = "./path/second_template.docx"
tpl_3_path = "./path/third_template.docx"
# destination
file_1_path = "./path/first_file.docx"
file_2_path = "./path/second_file.docx"
file_3_path = "./path/third_file.docx"

tpl_1 = DocxTemplate(tpl_1_path)
tpl_1.render({"content": ["item 1", "item 2"]})
tpl_1.save(file_1_path)

tpl_2 = DocxTemplate(tpl_2_path)
sub_file_1 = tpl_2.new_subdoc(file_1_path)
tpl_2.render({"FIRST_TEMPLATE": sub_file_1})
tpl_2.save(file_2_path)

tpl_3 = DocxTemplate(tpl_3_path)
sub_file_2 = tpl_3.new_subdoc(file_2_path)
tpl_3.render({"SECOND_TEMPLATE": sub_file_2})
tpl_3.save(file_3_path)

Expected behavior

With the exception of newline changes, all output files should be visually identical

Current behavior

First and second files show the same, a two element numbered list, but the 3rd file doesn't continue the numbering

Screenshots

image

Additional context

On my personal project, when going one layer deeper in subdocuments, the next step even erase the numbering property of the list and turn it into a bullet point list instead.

Sadly, I couldn't reproduce this behaviour as easily and my code and templates contains too much confidential informations to show as is and are too intricate to try to strip it down to a minimalist POC