Repository navigation
feat: open and save .docm, .dotx and .dotm files - #1644
Open
BenoitMarchand wants to merge 2 commits into
Open
BenoitMarchand wants to merge 2 commits into
BenoitMarchand wants to merge 2 commits into
Conversation
Accept the main-document content types of macro-enabled documents (.docm), templates (.dotx) and macro-enabled templates (.dotm) in addition to .docx. They share the .docx package structure; only the content type of the main document part differs. The package kind is preserved on save and the VBA project part is carried along unchanged. - add CONTENT_TYPE.WML_DOCUMENT_MACRO_MAIN, WML_TEMPLATE_MAIN and WML_TEMPLATE_MACRO_MAIN - register DocumentPart for those content types - accept them in docx.Document() - unit tests, acceptance scenarios (open, save/reload, VBA part kept), user docs, analysis page, HISTORY entry Fixes python-openxml#212, python-openxml#363. Addresses the file-format part of python-openxml#284 (macros are preserved, not editable). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- analysis page: how macros are stored in the package, implementation, experiments showing macros are preserved on save, limits and unintended consequences - doc-default.docm/.dotm fixtures now carry a real VBA project made by Word (PythonDocxCheck macro) instead of placeholder bytes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Open and save macro-enabled documents (
.docm), templates (.dotx) and macro-enabled templates (.dotm). The package kind is preserved on save and the VBA project is carried along byte for byte.Fixes #212, fixes #363. Addresses the file-format part of #284 (macros are preserved, not readable or editable).
Why another PR on this
Several PRs make the same code change (#673, #716, #1378, #1407). On #673 you said it could not be accepted without analysis documentation and tests, and that the analysis should cover how macros are stored in the package, document experiments showing they are preserved on save, and discuss where the approach may be insufficient or have unintended consequences. This PR adds those:
docs/dev/analysis/features/document-kinds.rst:vbaProject.bin,vbaData.xml, relationships, optional signature, keymap and ActiveX parts) and why they round-trip: they load as genericPartobjects,docx.Document()is parametrized over the four content types.api-open-document.feature:vbaProject.binis byte-identical..docm/.dotmfixtures carry a real VBA project saved by Word.documents.rst) and a HISTORY entry.Change
opc/constants.py: addWML_DOCUMENT_MACRO_MAIN,WML_TEMPLATE_MAINandWML_TEMPLATE_MACRO_MAIN.__init__.py: map those content types toDocumentPart.api.py: accept the four main-document content types.No new public API. Behaviour for
.docxis unchanged.Experiments (summary; details on the analysis page)
Word-made
.docm,.dotxand.dotmfiles covering most python-docx features (tables, images in 5 formats, comments, 4 sections with headers and footers) were used. The macro files contain a VBA module with a probe macro. Results:.docxopens; the other three raiseValueError: ... is not a Word file.vbaProject.binand every image are byte-identical;Checks
pytest: 1612 passedbehave: 656 scenarios passedruff checkandruff format --check: cleanpyright: no new errors🤖 Generated with Claude Code