Repository navigation
fix(docx): read styles with the invalid type "list" - #4831
Conversation
Some files declare a style with w:type="list" in styles.xml. ST_StyleType has no such value, and python-docx raises ValueError when it reads it. It reads the type of every style to find a default style: the backend does that when it opens the file (since 2.115), and the style of a paragraph without w:pStyle does it too. So one such style failed the whole conversion. Once the document is loaded, change the type to "numbering", the numbering style type (WD_STYLE_TYPE.LIST in python-docx). Signed-off-by: loickouao <diegoloic@hotmail.fr>
|
✅ DCO Check Passed Thanks @loickouao, all your commits are properly signed off. 🎉 |
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
AI triage (advisory)Checked commit The PR fixes a Topic labels added: CI safety: no concern found. A maintainer must still approve CI. 1 related pull request(s) in the same code
The |
There was a problem hiding this comment.
AI first review (advisory): no blocker found
This review covers the full diff at cfb5bd7158. It read the code and did not run it.
The fix correctly patches w:type="list" to "numbering" in the XML before python-docx reads the style type. The XML attribute name and replacement value are both verified against the python-docx source. The test covers the two failure paths described in the PR. No regressions or missing cases were found.
A maintainer makes the merge decision. This review never approves the pull request.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
A DOCX fails to convert with
ValueError: WD_STYLE_TYPE has no XML mapping for 'list'when itsstyles.xmldeclares a style withw:type="list"(#4830).listis not anST_StyleTypevalue, and python-docx raises when it reads it. To find a default style, python-docx reads the type of every style. The backend asks for the default paragraph style when it opens the file (since 2.115), andparagraph.styleasks for it on a paragraph withoutw:pStyle. Atry/exceptaround the call in__init__would leave the second path, so this PR changes the type tonumberingonce the document is loaded.numberingis the numbering style type (WD_STYLE_TYPE.LISTin python-docx). No other style type changes.Issue resolved by this Pull Request:
Resolves #4830
Checklist:
Review evidence:
test_list_style_type_does_not_fail_conversionbuilds the file at test time, once with the body paragraph styled and once withoutw:pStyle. Both cases fail onmainwith theValueErrorand pass on this branch. With only atry/exceptaroundstyles.default()in__init__, the case withoutw:pStylestill fails..docxundertests/datahas aliststyle, and the reference tests pass unchanged.tests/test_backend_msword*.pyandtests/test_office_image_modes.pypass.make validatepasses (ruff, ty, tach, max lines).MsWordDocumentBackend.__init__, and they merge without conflict.