diff --git a/docxtpl/subdoc.py b/docxtpl/subdoc.py index e8da093..994a16b 100644 --- a/docxtpl/subdoc.py +++ b/docxtpl/subdoc.py @@ -13,7 +13,6 @@ from docxcompose.composer import Composer from docxcompose.utils import NS from lxml import etree -import re class SubdocComposer(Composer): @@ -84,14 +83,10 @@ def __getattr__(self, name): def _get_xml(self): if self.subdocx.element.body.sectPr is not None: self.subdocx.element.body.remove(self.subdocx.element.body.sectPr) - xml = re.sub( - r"]*>", - "", - etree.tostring( - self.subdocx.element.body, encoding="unicode", pretty_print=False - ), + return "".join( + etree.tostring(element, encoding="unicode", pretty_print=False) + for element in self.subdocx.element.body ) - return xml def __unicode__(self): return self._get_xml() diff --git a/tests/pandoc_subdoc.py b/tests/pandoc_subdoc.py new file mode 100644 index 0000000..5aa2379 --- /dev/null +++ b/tests/pandoc_subdoc.py @@ -0,0 +1,32 @@ +import io +import zipfile + +from docx import Document +from lxml import etree + +from docxtpl import DocxTemplate + + +main_template = Document() +main_template.add_paragraph("{{p subdoc }}") +main_template_stream = io.BytesIO() +main_template.save(main_template_stream) +main_template_stream.seek(0) + +template = DocxTemplate(main_template_stream) +subdoc = template.new_subdoc("templates/pandoc_subdoc.docx") +template.render({"subdoc": subdoc}) +output_stream = io.BytesIO() +template.save(output_stream) + +output_stream.seek(0) +document = Document(output_stream) +assert len(document.inline_shapes) == 1 + +output_stream.seek(0) +with zipfile.ZipFile(output_stream) as archive: + document_xml = archive.read("word/document.xml") + +root = etree.fromstring(document_xml) +assert root.find(".//{http://schemas.openxmlformats.org/drawingml/2006/main}graphic") is not None +assert root.find(".//{http://schemas.openxmlformats.org/drawingml/2006/picture}pic") is not None diff --git a/tests/templates/pandoc_subdoc.docx b/tests/templates/pandoc_subdoc.docx new file mode 100644 index 0000000..0816b38 Binary files /dev/null and b/tests/templates/pandoc_subdoc.docx differ