diff --git a/docform/docx/subdoc.py b/docform/docx/subdoc.py index e8da093..331e85d 100644 --- a/docform/docx/subdoc.py +++ b/docform/docx/subdoc.py @@ -5,6 +5,8 @@ @author: Eric Lapouyade """ +from copy import deepcopy + from docx import Document from docx.oxml import CT_SectPr from docx.opc.constants import RELATIONSHIP_TYPE as RT @@ -13,7 +15,6 @@ from docxcompose.composer import Composer from docxcompose.utils import NS from lxml import etree -import re class SubdocComposer(Composer): @@ -82,16 +83,19 @@ def __getattr__(self, name): return getattr(self.subdocx, 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 xml + body = self.subdocx.element.body + if body.sectPr is not None: + body.remove(body.sectPr) + if len(body) == 0: + return "" + + destination_body = self.docx.element.body + wrapper = etree.Element(destination_body.tag, nsmap=destination_body.nsmap) + for element in body: + wrapper.append(deepcopy(element)) + + xml = etree.tostring(wrapper, encoding="unicode", pretty_print=False) + return xml.partition(">")[2].rpartition("%s' % (W_NS, fragment)).encode("utf-8") +) +assert len(fragment_body) == 10000 +assert all(element.tag == "{%s}p" % W_NS for element in fragment_body) 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