python-openxml / python-docx

Create and modify Word documents with Python
MIT License
4.38k stars 1.08k forks source link

Non compatibility of new update 1.1.1 with python-docx-template #1385

Closed lcpprop closed 2 months ago

lcpprop commented 2 months ago

I am using python-docx-template to generate a word document report by populating my word template. Yesterday evening (before update 1.1.1), I ran my code and everything was working smoothly. Now, in the morning (after update 1.1.1) and I get an error when running the same code:


AttributeError Traceback (most recent call last) File , line 90 89 template_vars {"name":"John", "lastname":"Oliver"} ---> 90 doc.render(template_vars) 92 doc.save("".docx")

File /local_disk0/.ephemeral_nfs/envs/.../lib/python3.10/site-packages/docxtpl/template.py:382, in DocxTemplate.render(self, context, jinja_env, autoescape) 379 # Headers 380 headers = self.build_headers_footers_xml(context, self.HEADER_URI, 381 jinja_env) --> 382 for relKey, xml in headers: 383 self.map_headers_footers_xml(relKey, xml) 385 # Footers

File /local_disk0/.ephemeral_nfs/envs/.../lib/python3.10/site-packages/docxtpl/template.py:338, in DocxTemplate.build_headers_footers_xml(self, context, uri, jinja_env) 337 def build_headers_footers_xml(self, context, uri, jinja_env=None): --> 338 for relKey, part in self.get_headers_footers(uri): 339 xml = self.get_part_xml(part) 340 encoding = self.get_headers_footers_encoding(xml)

File /local_disk0/.ephemeral_nfs/envs/.../lib/python3.10/site-packages/docxtpl/template.py:324, in DocxTemplate.get_headers_footers(self, uri) 323 def get_headers_footers(self, uri): --> 324 for relKey, val in self.docx._part._rels.items(): 325 if (val.reltype == uri) and (val.target_part.blob): 326 yield relKey, val.target_part

AttributeError: 'DocumentPart' object has no attribute '_rels'

Since, package python-docx-template specifies in their requirements.txt to install the latest version of all the required packages

six python-docx docxcompose jinja2 lxml"

and does not specify which version, the newest update has broken it. Now, that I specified that I want python-docx == 1.1.0, the code started working again. Please help.

scanny commented 2 months ago

@lcpprop I believe this error is due to the effect of this commit: https://github.com/python-openxml/python-docx/commit/6c34f128a5b5e331f1bbf88935f7c13396d33fb3#diff-df0c0602345350bde2a4ec6d48bb686ed0222ff3ae00130335d53575ab508d2cL28-L45

In particular, getting rid of the highlighted legacy implementation of @lazyproperty in docx.opc.shared and using the much newer and more robust implementation in docx.shared.

Unfortunately, it looks like DocxTemplate has relied on an internal feature of the former implementation (lazyproperty value for x was stored in self._x, so rels was stored in _rels) that has changed. Implementation details like this are subject to change which is why relying on them is always a risk.

I believe this can be fixed simply by dropping the leading underscore: s/_rels/rels.

Unfortunately I expect that will require a new release of python-docx-template to remedy.

scanny commented 2 months ago

@lcpprop I'm going to try adding a self._rels instance variable to Part which might get past this issue. I'm not at all sure there won't be another similar dependency on another @lazyproperty but I'm cutting a maintenance release anyway so it's worth a shot.

scanny commented 2 months ago

@lcpprop can you do a fresh install from the develop branch on GitHub and tell me if that works for you?

pip uninstall python-docx
pip install git+https://github.com/python-openxml/python-docx@develop

I added in a Part._rels instance attribute in that branch and that should get you past that error. Question is whether there's another error behind it :)

marcindulak commented 2 months ago

@lcpprop can you do a fresh install from the develop branch on GitHub and tell me if that works for you?

pip uninstall python-docx
pip install git+https://github.com/python-openxml/python-docx@develop

I added in a Part._rels instance attribute in that branch and that should get you past that error. Question is whether there's another error behind it :)

Also got AttributeError: 'DocumentPart' object has no attribute '_rels', and using today's python-docx@develop https://github.com/python-openxml/python-docx/commit/68844bccd1a1b30d4750b199a244ac7018cd1ead silences the error without apparent undesirable effects.

scanny commented 2 months ago

Thanks @marcindulak :) That's a big help to me :)

I'm still fixing a couple tests to support Python 3.12 but I hope to have the v1.1.2 release out today :)

scanny commented 2 months ago

Okay, v1.1.2 is up on PyPI :)