From 6d3d95f15cc4489fb093c3b0130d72b5d41a005c Mon Sep 17 00:00:00 2001 From: Shurong Cao <170531907+CAOShurong@users.noreply.github.com> Date: Sun, 22 Nov 2026 14:14:43 +0800 Subject: [PATCH 1/2] Preserve tabs and line breaks inside escaped delimiter text Port the rendering fix from elapouya/python-docx-template#662 (0a32c4b8). --- CHANGES.rst | 5 +++++ docform/docx/template.py | 9 ++++----- docs/index.rst | 3 +++ tests/unit/test_render.py | 20 +++++++++++++++++++- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 4e58b3e..99207fd 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,8 @@ +Unreleased +---------- + +- Preserve tabs and line breaks between the characters of an escaped delimiter in Word templates. + 0.21.0 (2026-11-15) ------------------- First release as Docform. Rendering output matches docxtpl 0.20.2. diff --git a/docform/docx/template.py b/docform/docx/template.py index c3ed094..469d10e 100644 --- a/docform/docx/template.py +++ b/docform/docx/template.py @@ -87,11 +87,10 @@ def patch_xml(self, src_xml): strip all unnecessary xml tags, manage table cell background color and colspan, unescape html entities, etc...""" - # Join escaped Jinja delimiters that Word has split across runs. Limit the - # removable XML to tags inside one paragraph so unrelated text is untouched. - split_run_gap = ( - r"(?:(?:(?!))<[^>]+>)*]*)?>)?" - ) + # Between adjacent text nodes, allow run boundaries and font properties. + # Other elements, such as tabs and line breaks, interrupt the delimiter. + run_markup = r"]*>||(?:<[^<>]+/>)*" + split_run_gap = rf"(?:(?:{run_markup})*]*>)?" for escaped_delimiter in ("{_{", "}_}", "{_%", "%_}"): pattern = split_run_gap.join(map(re.escape, escaped_delimiter)) src_xml = re.sub(pattern, escaped_delimiter, src_xml) diff --git a/docs/index.rst b/docs/index.rst index a0c5763..fb5eed5 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -186,6 +186,9 @@ In order to display ``{%``, ``%}``, ``{{`` or ``}}``, one can use:: {_%, %_}, {_{ or }_} +The characters of an escaped delimiter can have different Word formatting. +A tab or line break between the characters interrupts the delimiter and is preserved. + Tables ...... diff --git a/tests/unit/test_render.py b/tests/unit/test_render.py index 82b32d7..5d3dedd 100644 --- a/tests/unit/test_render.py +++ b/tests/unit/test_render.py @@ -1,4 +1,5 @@ import pytest +from docx import Document from jinja2 import Environment from jinja2.exceptions import TemplateSyntaxError, UndefinedError from jinja2 import StrictUndefined @@ -15,6 +16,24 @@ def test_variables(make_docx): assert body_text(saved(tpl)) == ["Hello Ada", "Total: $12"] +@pytest.mark.parametrize( + ("separator", "expected"), [("", "}}"), ("\t", "}\t_}"), ("\n", "}\n_}")] +) +def test_escaped_delimiter_across_runs(tmp_path, separator, expected): + doc = Document() + paragraph = doc.add_paragraph() + paragraph.add_run("left }") + paragraph.add_run(separator) + paragraph.add_run("_").bold = True + paragraph.add_run("} right") + path = tmp_path / "delimiter.docx" + doc.save(path) + + tpl = DocxTemplate(path) + tpl.render({}) + assert body_text(saved(tpl)) == ["left " + expected + " right"] + + def test_missing_variable_renders_empty(make_docx): tpl = DocxTemplate(make_docx(["Hi {{ missing }}!"])) tpl.render({}) @@ -142,7 +161,6 @@ def test_save_to_path(make_docx, tmp_path): tpl.render({"a": "ok"}) out = tmp_path / "out.docx" tpl.save(out) - from docx import Document assert body_text(Document(str(out))) == ["ok"] From 484c8598894ec0cbf9475d9f84de7f36715df5c0 Mon Sep 17 00:00:00 2001 From: CAOShurong <170531907+CAOShurong@users.noreply.github.com> Date: Sun, 22 Nov 2026 16:51:13 +0800 Subject: [PATCH 2/2] Allow Word proofing and layout marks within escaped delimiters --- docform/docx/template.py | 8 ++++---- tests/unit/test_render.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/docform/docx/template.py b/docform/docx/template.py index 469d10e..3e19f91 100644 --- a/docform/docx/template.py +++ b/docform/docx/template.py @@ -87,10 +87,10 @@ def patch_xml(self, src_xml): strip all unnecessary xml tags, manage table cell background color and colspan, unescape html entities, etc...""" - # Between adjacent text nodes, allow run boundaries and font properties. - # Other elements, such as tabs and line breaks, interrupt the delimiter. - run_markup = r"]*>||(?:<[^<>]+/>)*" - split_run_gap = rf"(?:(?:{run_markup})*]*>)?" + # Word may insert proofing marks, bookmarks or run formatting between + # text nodes. Paragraph boundaries, tabs and line breaks end a delimiter. + boundary = r"])" + split_run_gap = rf"(?:(?:(?!{boundary})<[^>]+>)*]*)?>)?" for escaped_delimiter in ("{_{", "}_}", "{_%", "%_}"): pattern = split_run_gap.join(map(re.escape, escaped_delimiter)) src_xml = re.sub(pattern, escaped_delimiter, src_xml) diff --git a/tests/unit/test_render.py b/tests/unit/test_render.py index 5d3dedd..694a888 100644 --- a/tests/unit/test_render.py +++ b/tests/unit/test_render.py @@ -1,5 +1,7 @@ import pytest from docx import Document +from docx.oxml import OxmlElement +from docx.oxml.ns import qn from jinja2 import Environment from jinja2.exceptions import TemplateSyntaxError, UndefinedError from jinja2 import StrictUndefined @@ -34,6 +36,23 @@ def test_escaped_delimiter_across_runs(tmp_path, separator, expected): assert body_text(saved(tpl)) == ["left " + expected + " right"] +def test_escaped_delimiter_across_proofing_marks(tmp_path): + doc = Document() + paragraph = doc.add_paragraph() + paragraph.add_run("left }") + for mark, text in (("spellStart", "_"), ("spellEnd", "} right")): + marker = OxmlElement("w:proofErr") + marker.set(qn("w:type"), mark) + paragraph._p.append(marker) + paragraph.add_run(text) + path = tmp_path / "proofing.docx" + doc.save(path) + + tpl = DocxTemplate(path) + tpl.render({}) + assert body_text(saved(tpl)) == ["left }} right"] + + def test_missing_variable_renders_empty(make_docx): tpl = DocxTemplate(make_docx(["Hi {{ missing }}!"])) tpl.render({})