Skip to content
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

Add support for word documents with macro's #1407

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/docx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def part_class_selector(content_type: str, reltype: str) -> Type[Part] | None:
PartFactory.part_class_selector = part_class_selector
PartFactory.part_type_for[CT.OPC_CORE_PROPERTIES] = CorePropertiesPart
PartFactory.part_type_for[CT.WML_DOCUMENT_MAIN] = DocumentPart
PartFactory.part_type_for[CT.WML_DOCUMENT_MAIN_WITH_MACROS] = DocumentPart
PartFactory.part_type_for[CT.WML_FOOTER] = FooterPart
PartFactory.part_type_for[CT.WML_HEADER] = HeaderPart
PartFactory.part_type_for[CT.WML_NUMBERING] = NumberingPart
Expand Down
2 changes: 1 addition & 1 deletion src/docx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def Document(docx: str | IO[bytes] | None = None) -> DocumentObject:
"""
docx = _default_docx_path() if docx is None else docx
document_part = cast("DocumentPart", Package.open(docx).main_document_part)
if document_part.content_type != CT.WML_DOCUMENT_MAIN:
if document_part.content_type not in (CT.WML_DOCUMENT_MAIN, CT.WML_DOCUMENT_MAIN_WITH_MACROS):
tmpl = "file '%s' is not a Word file, content type is '%s'"
raise ValueError(tmpl % (docx, document_part.content_type))
return document_part.document
Expand Down
3 changes: 3 additions & 0 deletions src/docx/opc/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ class CONTENT_TYPE:
"application/vnd.openxmlformats-officedocument.wordprocessingml.docu"
"ment.main+xml"
)
WML_DOCUMENT_MAIN_WITH_MACROS = (
"application/vnd.ms-word.document.macroEnabled.main+xml"
)
WML_ENDNOTES = (
"application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml"
)
Expand Down