Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions docxtpl/subdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from docxcompose.composer import Composer
from docxcompose.utils import NS
from lxml import etree
import re


class SubdocComposer(Composer):
Expand Down Expand Up @@ -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"</?w:body[^>]*>",
"",
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()
Expand Down
32 changes: 32 additions & 0 deletions tests/pandoc_subdoc.py
Original file line number Diff line number Diff line change
@@ -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
Binary file added tests/templates/pandoc_subdoc.docx
Binary file not shown.