Repository navigation
Description
Copied from python-docx-template issue 537, opened by @Daniel-Sennewald on 2024-04-16.
Describe the bug
When creating subdocs with doc.new_subdoc() and opening the resulting docx file with LibreOffice, the subdoc is not visibile.
When opening the SAME .docx file with word, the subdoc content is correctly shown.
To Reproduce
Note: .docx files are attached.
import docxtpl
main_doc_context = {
"test": "main doc test text",
}
sub_doc_context = {
"test": "sub doc test text",
}
subdoc = docxtpl.DocxTemplate("./test_libre_sub.docx")
subdoc.render(sub_doc_context)
subdoc.save("./test_libre_sub_rendered.docx")
main_doc = docxtpl.DocxTemplate("./test_libre.docx")
rendered_subdoc = main_doc.new_subdoc("./test_libre_sub_rendered.docx")
main_doc_context["subdoc1"] = rendered_subdoc
main_doc.render(main_doc_context)
main_doc.save("./test_libre_rendered.docx")test_libre_sub.docx
test_libre.docx
Open the file test_libre_rendered.docx with Word and with LibreOffice. Word renders the subdoc contents, LibreOffice does not.
Expected behavior
When opening the doc with either libreoffice the subdoc should be rendered.
Additional Info
Note that the subdoc itself ("./test_libre_sub_rendered.docx") correctly includes the rendered subdoc.
Tested on:
Mac (Intel) x64
OpenOffice: 4.1.15
LibreOffice: 24.2.2
Comments from the original thread
@tdaudi on 2024-05-07:
Up
The example given in the tests does not work for me. When I merge the docx I have just not visible content in the file.
The content is not visible using LibreOffice/Google Docs/Word, BUT it does actually appear inside the XML data. Which means that it is being written in the doc but probably the wrong way ?
@tdaudi on 2024-05-07:
@elapouya sorry for the ping, I have seen you wrote the part of the code related to merging documents.
Do you have any hint to give us for this bug ?
I'm using python 3.12.3 and docxtpl version 0.17.0
Thanks
@elapouya on 2024-05-08:
It requires good understanding of docx format to answer your question. Unfortunately I do not have sufficient knowledge about that, sorry
Le mar. 7 mai 2024 à 12:06, ScriptSathi @.***(mailto:Le mar. 7 mai 2024 à 12:06, ScriptSathi < a écrit :
@.***(https://github.com/elapouya) sorry for the ping, I have seen you wrote the part of the code related to merging documents.
Do you have any hint to give us for this bug ?I'm using python 3.12.3 and docxtpl version 0.17.0
Thanks
—
Reply to this email directly, view it on GitHub (https://github.com/elapouya/python-docx-template/issues/537#issuecomment-2097931489), or unsubscribe.
You are receiving this because you were mentioned.Message ID: @.***>
@pemontto on 2024-09-22:
Running into this as well. It renders in Word, but interestingly when exporting from Sharepoint as a PDF it goes missing. Opening and printing to PDF retains it.
@pemontto on 2024-09-23:
OK so for me, I found that I can only see subdocuments when included with a {{p subdoc }}. However when I would insert a subdoc with a table with {{p subdoc }} it would break MS Word. Using {{ subdoc }} would work in word but break everything else. The solution I've found to work is to ensure the last element in the subdoc is a paragraph so {{p subdoc }} works.
This may be brittle but worked for me 🤷♂️:
from docx.oxml.text.paragraph import CT_P
# Don't finish run without a paragraph, it corrupts the xml
elements = list(subdoc.element.body)
if len(elements) > 1 and not isinstance(elements[-2], CT_P):
subdoc.add_paragraph()@Alkalit on 2025-04-24:
@pemontto, God bless you, friend.
@amaurylrd on 2026-03-04:
@pemontto merci pour les travaux
Activity
cvas22 commentedon Nov 22, 2026
@Daniel-Sennewald @tdaudi @pemontto two things here. Your template uses
{{ subdoc1 }}, which puts the subdoc's paragraphs inside a text run, Word tolerates it but LibreOffice doesn't, so use{{p subdoc1 }}(documented in #180). And pemontto's case, a subdoc ending with a table breaking Word under{{p }}, is fixed in #188, a paragraph gets added after the table now.