Skip to content
Closed
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
7 changes: 6 additions & 1 deletion docxtpl/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ def colspan(m):

# manage table cell background color
def cellbg(m):
cell_xml = m.group(1) + m.group(3)
cell_xml = re.sub(
r"<w:tcPr(\s[^>]*)?/>",
r"<w:tcPr\1></w:tcPr>",
m.group(1) + m.group(3),
count=1,
)
cell_xml = re.sub(
r"<w:r[ >](?:(?!<w:r[ >]).)*<w:t></w:t>.*?</w:r>",
"",
Expand Down
21 changes: 21 additions & 0 deletions tests/cellbg_shorthand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from lxml import etree

from docxtpl import DocxTemplate


WORD_NAMESPACE = (
"http://schemas.openxmlformats.org/wordprocessingml/2006/main"
)
SOURCE_XML = f"""
<w:tc xmlns:w="{WORD_NAMESPACE}">
<w:tcPr />
<w:p><w:r><w:t>{{% cellbg background %}}content</w:t></w:r></w:p>
</w:tc>
"""

patched_xml = DocxTemplate(None).patch_xml(SOURCE_XML)
cell = etree.fromstring(patched_xml.encode())
namespaces = {"w": WORD_NAMESPACE}

assert len(cell.xpath("./w:tcPr/w:shd", namespaces=namespaces)) == 1
assert len(cell.xpath("./w:shd", namespaces=namespaces)) == 0