Skip to content
Merged
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
60 changes: 60 additions & 0 deletions docform/docx/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ def render(

# fix docPr ID's
self.fix_docpr_ids(tree)
self.fix_shape_ids(tree)

# Replace body xml tree
self.drop_duplicate_bookmarks(tree)
Expand Down Expand Up @@ -1070,6 +1071,65 @@ def fix_docpr_ids(self, tree):
self.docx_ids_index += 1
elt.attrib["id"] = str(self.docx_ids_index)

SHAPE_ANCHOR_IDS = (
"{http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing}anchorId",
"{http://schemas.microsoft.com/office/word/2010/wordml}anchorId",
)
VML_SHAPE = "{urn:schemas-microsoft-com:vml}shape"
VML_SPID = "{urn:schemas-microsoft-com:office:office}spid"
OLE_OBJECT = "{urn:schemas-microsoft-com:office:office}OLEObject"

def fix_shape_ids(self, tree):
"""Give copies of a shape or text box their own ids.

A loop repeats a shape's anchorId and its VML fallback's id and
o:spid, and Word reports such a document as corrupted. The first
copy keeps its ids, later copies get new unique ones.
"""
elements = [el for el in tree.iter() if isinstance(el.tag, str)]

def dedupe(items, get, put, make):
used = {get(el) for el in items}
seen = set()
index = 1
for el in items:
value = get(el)
if value not in seen:
seen.add(value)
continue
while make(index) in used:
index += 1
new = make(index)
used.add(new)
put(el, value, new)

for attr in self.SHAPE_ANCHOR_IDS:
dedupe(
[el for el in elements if el.get(attr) is not None],
lambda el, a=attr: el.get(a).upper(),
lambda el, old, new, a=attr: el.set(a, new),
lambda index: "%08X" % (0x10000000 + index),
)

shapes = [
el for el in elements if el.tag == self.VML_SHAPE and el.get("id") is not None
]

def set_shape_id(shape, old, new):
shape.set("id", new)
parent = shape.getparent()
for ole in [] if parent is None else parent.iter(self.OLE_OBJECT):
if ole.get("ShapeID") == old:
ole.set("ShapeID", new)

dedupe(shapes, lambda el: el.get("id"), set_shape_id, lambda i: "_x0000_s%d" % (3000 + i))
dedupe(
[el for el in shapes if el.get(self.VML_SPID) is not None],
lambda el: el.get(self.VML_SPID),
lambda el, old, new: el.set(self.VML_SPID, new),
lambda index: "_x0000_s%d" % (4000 + index),
)

def new_subdoc(self, docpath=None) -> Subdoc:
try:
from .subdoc import Subdoc
Expand Down
56 changes: 56 additions & 0 deletions tests/unit/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,59 @@ def test_richtext_carriage_return_becomes_line_break(make_docx):
tpl = DocxTemplate(make_docx(["{{r rt }}"]))
tpl.render({"rt": RichText("a\r\nb\rc", bold=True)})
assert saved(tpl).paragraphs[0]._p.xml.count("<w:br/>") == 2


TEXT_BOX_XML = (
'<w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"'
' xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"'
' xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"'
' xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"'
' xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"'
' xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"'
' xmlns:v="urn:schemas-microsoft-com:vml"'
' xmlns:o="urn:schemas-microsoft-com:office:office">'
'<mc:AlternateContent><mc:Choice Requires="wps"><w:drawing>'
'<wp:anchor distT="0" distB="0" distL="0" distR="0" simplePos="0" relativeHeight="1"'
' behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1"'
' wp14:anchorId="1A2B3C4D" wp14:editId="5E6F7A8B">'
'<wp:simplePos x="0" y="0"/>'
'<wp:positionH relativeFrom="column"><wp:posOffset>0</wp:posOffset></wp:positionH>'
'<wp:positionV relativeFrom="paragraph"><wp:posOffset>0</wp:posOffset></wp:positionV>'
'<wp:extent cx="914400" cy="457200"/><wp:effectExtent l="0" t="0" r="0" b="0"/>'
'<wp:wrapNone/><wp:docPr id="7" name="Text Box 7"/><wp:cNvGraphicFramePr/>'
'<a:graphic><a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">'
'<wps:wsp><wps:cNvSpPr txBox="1"/><wps:spPr/>'
'<wps:txbx><w:txbxContent><w:p><w:r><w:t>box</w:t></w:r></w:p></w:txbxContent></wps:txbx>'
'<wps:bodyPr/></wps:wsp></a:graphicData></a:graphic></wp:anchor>'
'</w:drawing></mc:Choice><mc:Fallback><w:pict>'
'<v:shape id="Text Box 7" o:spid="_x0000_s1026" type="#_x0000_t202">'
'<v:textbox><w:txbxContent><w:p><w:r><w:t>box</w:t></w:r></w:p></w:txbxContent></v:textbox>'
'</v:shape></w:pict></mc:Fallback></mc:AlternateContent></w:r>'
)


def test_text_box_in_loop_gets_unique_ids(tmp_path):
from docx.oxml import parse_xml

doc = Document()
doc.add_paragraph("{%p for x in xs %}")
doc.add_paragraph()._p.append(parse_xml(TEXT_BOX_XML))
doc.add_paragraph("{%p endfor %}")
path = tmp_path / "textbox.docx"
doc.save(str(path))

tpl = DocxTemplate(str(path))
tpl.render({"xs": [1, 2, 3]})
body = saved(tpl).element.body
ns = {
"wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
"wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
"v": "urn:schemas-microsoft-com:vml",
"o": "urn:schemas-microsoft-com:office:office",
}
for path_ in (".//wp:anchor/@wp14:anchorId", ".//v:shape/@id", ".//v:shape/@o:spid", ".//wp:docPr/@id"):
values = etree._Element.xpath(body, path_, namespaces=ns)
assert len(values) == 3, path_
assert len(set(values)) == 3, (path_, values)
# the first copy keeps the ids from the template
assert etree._Element.xpath(body, "(.//v:shape)[1]/@o:spid", namespaces=ns) == ["_x0000_s1026"]
Loading