Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lcpprop opened this issue Apr 30, 2024 · 6 comments · Fixed by BlackBoiler/python-docx#23
Closed

Comments

@lcpprop
Copy link

lcpprop commented Apr 30, 2024

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("<path_to_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
Copy link
Contributor

scanny commented Apr 30, 2024

@lcpprop I believe this error is due to the effect of this commit:
6c34f12#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
Copy link
Contributor

scanny commented Apr 30, 2024

@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
Copy link
Contributor

scanny commented Apr 30, 2024

@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
Copy link

@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 68844bc silences the error without apparent undesirable effects.

@scanny
Copy link
Contributor

scanny commented May 1, 2024

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
Copy link
Contributor

scanny commented May 1, 2024

Okay, v1.1.2 is up on PyPI :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants