pythondocxtpl

python docxtpl insert two sub documents into one template


I would like to insert two generated sub documents into one template. It partly works, but it seems that there is a bug in docxtpl. The end of my template disappears if I insert my sub document(s). See the following example:

The template.docx has the following content:

some text

{{p subdoc1}}

again some text

{{p subdoc2}}

and at the end of the document also some text

My sample code is:

from docxtpl import DocxTemplate

doc = DocxTemplate("docx_template.docx")

sd1 = doc.new_subdoc()
sd1.add_paragraph("First subdoc")

sd2 = doc.new_subdoc()
sd2.add_paragraph("Second subdoc")

context = {"subdoc1" : sd1, "subdoc2" : sd2}

doc.render(context)
doc.save("output.docx")

What I expect in output.docx is:

some text

First subdoc

again some text

Second subdoc

and at the end of the document also some text

What I get is:

some text

First subdoc

the second sub document and the end of the documents text is not in the saved output.docx.
What am I doing wrong?

I also run the test subdoc.py from https://github.com/elapouya/python-docx-template/tree/master/tests. The end of the generated document also disappears. I'm not sure if this is a wanted feature or maybe a bug.

My versions are:


Thanks in advance,
Thomas

Solution

  • I have tried the same project that you posted and I am able to get the desired result.

    My used versions are:

    Can you try with docxtpl version 0.16.6 and see any difference.