From 2b2628c6df1884be9d87e3a6f288b2106c1fff80 Mon Sep 17 00:00:00 2001 From: wafi543 Date: Mon, 19 Apr 2021 12:35:38 +0300 Subject: [PATCH 1/3] fix #345 --- docxtpl/__init__.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 734e2d5..231a0e3 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -318,6 +318,9 @@ class DocxTemplate(object): # fix tables if needed tree = self.fix_tables(xml_src) + # fix shapes if needed + tree = self.fix_shapes(xml_src) + self.map_tree(tree) # Headers @@ -332,6 +335,52 @@ class DocxTemplate(object): for relKey, xml in footers: self.map_headers_footers_xml(relKey, xml) + def fix_shapes(self, xml): + parser = etree.XMLParser(recover=True) + tree = etree.fromstring(xml, parser=parser) + # get namespace + ns = '{' + tree.nsmap['w'] + '}' + nsp = '{' + tree.nsmap['wp'] + '}' + mc = '{' + tree.nsmap['mc'] + '}' + + # walk trough xml and fix docPr id duplicates + ids = [] + amount = 2000 + for p in tree.iter(ns+'p'): + rows = p.findall(ns+'r') + for r in rows: + drawing = r.find(ns+'drawing') + alternative = r.find(mc+'AlternateContent') + if drawing is not None: + anchor = drawing.find(nsp+'anchor') + if anchor is not None: + docPr = anchor.find(nsp+'docPr') + id = docPr.attrib['id'] + if not id in ids: + ids.append(id) + else: + docPr.set('id', str(int(id)+amount)) + elif alternative is not None: + choice = alternative.find(mc+'Choice') + drawing = choice.find(ns+'drawing') + anchor = drawing.find(nsp+'anchor') + inline = drawing.find(nsp+'inline') + if anchor is not None: + docPr = anchor.find(nsp+'docPr') + id = docPr.attrib['id'] + if not id in ids: + ids.append(id) + else: + docPr.set('id', str(int(id)+amount)) + elif inline is not None: + docPr = inline.find(nsp+'docPr') + id = docPr.attrib['id'] + if not id in ids: + ids.append(id) + else: + docPr.set('id', str(int(id)+amount)) + return tree + # using of TC tag in for cycle can cause that count of columns does not # correspond to real count of columns in row. This function is able to fix it. def fix_tables(self, xml): From 8d512d2eddf378a8dc4c53cf469c5255f841b678 Mon Sep 17 00:00:00 2001 From: wafi543 Date: Mon, 19 Apr 2021 12:49:04 +0300 Subject: [PATCH 2/3] Flake8 Rules --- docxtpl/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 231a0e3..a199ec1 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -356,7 +356,7 @@ class DocxTemplate(object): if anchor is not None: docPr = anchor.find(nsp+'docPr') id = docPr.attrib['id'] - if not id in ids: + if id not in ids: ids.append(id) else: docPr.set('id', str(int(id)+amount)) @@ -368,14 +368,14 @@ class DocxTemplate(object): if anchor is not None: docPr = anchor.find(nsp+'docPr') id = docPr.attrib['id'] - if not id in ids: + if id not in ids: ids.append(id) else: docPr.set('id', str(int(id)+amount)) elif inline is not None: docPr = inline.find(nsp+'docPr') id = docPr.attrib['id'] - if not id in ids: + if id not in ids: ids.append(id) else: docPr.set('id', str(int(id)+amount)) From 6b41cc25799433fb2aa6be40dc38067bc5a82107 Mon Sep 17 00:00:00 2001 From: wafi543 Date: Mon, 19 Apr 2021 14:17:38 +0300 Subject: [PATCH 3/3] continued fix #345 --- docxtpl/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index a199ec1..1b528a4 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -360,6 +360,7 @@ class DocxTemplate(object): ids.append(id) else: docPr.set('id', str(int(id)+amount)) + amount += 1000 elif alternative is not None: choice = alternative.find(mc+'Choice') drawing = choice.find(ns+'drawing') @@ -372,6 +373,7 @@ class DocxTemplate(object): ids.append(id) else: docPr.set('id', str(int(id)+amount)) + amount += 1000 elif inline is not None: docPr = inline.find(nsp+'docPr') id = docPr.attrib['id'] @@ -379,6 +381,7 @@ class DocxTemplate(object): ids.append(id) else: docPr.set('id', str(int(id)+amount)) + amount += 1000 return tree # using of TC tag in for cycle can cause that count of columns does not