You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unwanted empty paragraph symbol after every sub document in a loop #628
I want to insert multiple sub documents in a loop. I can get this to work, but there is always an extra paragraph symbol after each insertion that I do not want to be there.
More details about your problem
This is the result that I am getting:
And this is the result that I want to get / am expecting:
Provide a test case
Python code:
importjsonfromdocxtplimportDocxTemplatefromjinja2importEnvironment, UndefinedclassCustomUndefined(Undefined):
def__str__(self):
returnf"[[{self._undefined_name}]]"defmain():
# Setup stuffjenv=Environment(undefined=CustomUndefined)
withopen("json/test-example.json", "r") asctx_file:
ctx: dict=json.load(ctx_file)
# Initialize the master documentdoc=DocxTemplate("templates/Main.docx")
# Deal with the nested insertsubdocs= []
fordatainctx.get("houses", []):
sub_doc_tpl=DocxTemplate("templates/Property.docx")
sub_doc_tpl.render(context=data, jinja_env=jenv)
# Create a new subdoc, and replace the content with the content from the rendered template itselfsd=doc.new_subdoc()
sd.subdocx=sub_doc_tpl.docxsubdocs.append(sd)
ctx.update({"subdocs": {"houses": subdocs}})
# Render and save the master documentdoc.render(context=ctx, jinja_env=jenv)
doc.save("./output/generated_doc.docx")
if__name__=="__main__":
main()
And this is the json file stored at ./json/test-example.json which my python code is reading in as the context.
I reproduced this on current master (1f143fb) with two generated subdocuments. The template screenshot uses {{ item }}, which inserts the subdocument XML inside the placeholder paragraph and leaves that paragraph behind. With two items, the rendered paragraph texts were:
['Before', '', '', 'After']
Subdocuments need the paragraph-level tag documented for this feature. Changing only the placeholder line to:
{{p item }}
while keeping the surrounding {%p for ... %} / {%p endfor %} lines produced:
[
'Before',
'Property first paragraph',
'Property second paragraph',
'Property first paragraph',
'Property second paragraph',
'After',
]
So {{p item }} removes the placeholder paragraph and inserts the subdocument paragraphs at the correct level, eliminating the extra paragraph after each item. No library change appears necessary for the shown template.
Activity
blismatic commentedon Dec 30, 2025
Just bumping this issue as it has been 2 weeks with no response. Anyone have any clue how to fix this?
blismatic commentedon Apr 6, 2026
Bumping this issue one more time. Hoping someone can help me address this.
ahmetmusab42-stack commentedon Sep 1, 2026
I reproduced this on current
master(1f143fb) with two generated subdocuments. The template screenshot uses{{ item }}, which inserts the subdocument XML inside the placeholder paragraph and leaves that paragraph behind. With two items, the rendered paragraph texts were:Subdocuments need the paragraph-level tag documented for this feature. Changing only the placeholder line to:
{{p item }}while keeping the surrounding
{%p for ... %}/{%p endfor %}lines produced:[ 'Before', 'Property first paragraph', 'Property second paragraph', 'Property first paragraph', 'Property second paragraph', 'After', ]So
{{p item }}removes the placeholder paragraph and inserts the subdocument paragraphs at the correct level, eliminating the extra paragraph after each item. No library change appears necessary for the shown template.