From d3ddb4840338ef350a8de8d76dbd3221f2bd38b7 Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Sun, 22 Nov 2026 02:27:51 -0800 Subject: [PATCH] Add a paragraph after a subdoc's final table When a subdoc ends with a table and goes in with {{p subdoc }} at the end of the document or of a table cell, the table ended up last, and Word rejects a cell or body that doesn't end with a paragraph. Append an empty paragraph in that case, which is what Word does itself. Fixes #106 --- CHANGES.rst | 2 ++ docform/docx/subdoc.py | 4 ++++ tests/unit/test_objects.py | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 99f796d..d10f89c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -15,6 +15,8 @@ Unreleased properties like ``w:textAlignment``, and ``{%-``/``-%}`` next to a table no longer drop its tags. - Templates saved by LibreOffice no longer get a second ``docProps/core.xml``, which made LibreOffice refuse the generated file. +- A subdoc that ends with a table gets an empty paragraph after it, so ``{{p subdoc }}`` at the + end of the document or of a table cell no longer makes Word reject the file. 0.21.0 (2026-11-15) ------------------- diff --git a/docform/docx/subdoc.py b/docform/docx/subdoc.py index 5ff351f..deccadc 100644 --- a/docform/docx/subdoc.py +++ b/docform/docx/subdoc.py @@ -171,6 +171,10 @@ def _get_xml(self): wrapper = etree.Element(destination_body.tag, nsmap=destination_body.nsmap) for element in body: wrapper.append(deepcopy(element)) + # Word needs a paragraph after a table at the end of a cell or of the + # document, and {{p subdoc }} can put the subdoc's last table there. + if wrapper[-1].tag == "{%s}tbl" % nsmap["w"]: + etree.SubElement(wrapper, "{%s}p" % nsmap["w"]) xml = etree.tostring(wrapper, encoding="unicode", pretty_print=False) return xml.partition(">")[2].rpartition("