diff --git a/CHANGES.rst b/CHANGES.rst index f7e31f4..53ce978 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,7 @@ +0.7.0 (2020-04-09) +------------------- +- Add replace_zipname() method to replace Excel and PowerPoint embedded files + 0.6.4 (2020-04-06) ------------------- - Add the possibility to add RichText to a Richtext diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 9ac0cd3..8be126a 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -7,12 +7,11 @@ Created : 2015-03-12 import functools import io -__version__ = '0.6.9' +__version__ = '0.7.0' from lxml import etree from docx import Document from docx.opc.oxml import serialize_part_xml, parse_xml -from docx.opc.part import XmlPart import docx.oxml.ns from docx.opc.constants import RELATIONSHIP_TYPE as REL_TYPE from jinja2 import Environment, Template, meta @@ -238,8 +237,8 @@ class DocxTemplate(object): def get_headers_footers_xml(self, uri): for relKey, val in self.docx._part._rels.items(): - if (val.reltype == uri) and (val.target_part.blob): - yield relKey, self.xml_to_string(parse_xml(val.target_part.blob)) + if (val.reltype == uri) and (val._target._blob): + yield relKey, self.xml_to_string(parse_xml(val._target._blob)) def get_headers_footers_encoding(self,xml): m = re.match(r'<\?xml[^\?]+\bencoding="([^"]+)"',xml,re.I) @@ -255,8 +254,7 @@ class DocxTemplate(object): yield relKey, xml.encode(encoding) def map_headers_footers_xml(self, relKey, xml): - part = self.docx._part._rels[relKey].target_part - self.docx._part._rels[relKey]._target = XmlPart.load(part.partname, part.content_type, xml, part.package) + self.docx._part._rels[relKey]._target._blob = xml def render(self, context, jinja_env=None, autoescape=False): if autoescape: @@ -510,8 +508,11 @@ class DocxTemplate(object): if item.filename in self.zipname_to_replace: zout.writestr(item, self.zipname_to_replace[item.filename]) elif ( item.filename.startswith('word/media/') and - item.CRC in self.crc_to_new_media ): + item.CRC in self.crc_to_new_media ): zout.writestr(item, self.crc_to_new_media[item.CRC]) + elif ( item.filename.startswith('word/embeddings/') and + item.CRC in self.crc_to_new_embedded): + zout.writestr(item, self.crc_to_new_embedded[item.CRC]) else: zout.writestr(item, buf) diff --git a/setup.py b/setup.py index 42a559e..b5b3819 100644 --- a/setup.py +++ b/setup.py @@ -59,7 +59,7 @@ setup(name='docxtpl', license='LGPL 2.1', packages=['docxtpl'], install_requires=['six', - 'python-docx', + 'python-docx<=0.8.7', # newer docx package breaks header/footer management in docxtpl 'jinja2', 'lxml'], extras_require={'docs': ['Sphinx', 'sphinxcontrib-napoleon']}, diff --git a/tests/templates/embedded.docx b/tests/templates/embedded.docx deleted file mode 100644 index 6ab8e60..0000000 Binary files a/tests/templates/embedded.docx and /dev/null differ diff --git a/tests/templates/embedded_dummy.docx b/tests/templates/embedded_dummy.docx index 48a2663..2aede3e 100644 Binary files a/tests/templates/embedded_dummy.docx and b/tests/templates/embedded_dummy.docx differ diff --git a/tests/templates/embedded_embedded_docx.docx b/tests/templates/embedded_embedded_docx.docx deleted file mode 100644 index e69de29..0000000 diff --git a/tests/templates/embedded_main_tpl.docx b/tests/templates/embedded_main_tpl.docx index 923271a..8f5dab6 100644 Binary files a/tests/templates/embedded_main_tpl.docx and b/tests/templates/embedded_main_tpl.docx differ