From d237c36f71365ef8c8bc75d339129ca9805a11aa Mon Sep 17 00:00:00 2001 From: Erlend ter Maat Date: Fri, 14 Jun 2024 17:13:01 +0200 Subject: [PATCH 1/2] Add support for word documents with macro's --- src/docx/__init__.py | 1 + src/docx/api.py | 2 +- src/docx/opc/constants.py | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/docx/__init__.py b/src/docx/__init__.py index 205221027..069d98a97 100644 --- a/src/docx/__init__.py +++ b/src/docx/__init__.py @@ -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 diff --git a/src/docx/api.py b/src/docx/api.py index aea876458..8c558e07f 100644 --- a/src/docx/api.py +++ b/src/docx/api.py @@ -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, 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 diff --git a/src/docx/opc/constants.py b/src/docx/opc/constants.py index 89d3c16cc..2982ef189 100644 --- a/src/docx/opc/constants.py +++ b/src/docx/opc/constants.py @@ -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" ) From d28eaa1c2d6dcf70412917989b69cc3c43a0763d Mon Sep 17 00:00:00 2001 From: Erlend ter Maat Date: Fri, 14 Jun 2024 17:57:00 +0200 Subject: [PATCH 2/2] Bugfix --- src/docx/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/docx/api.py b/src/docx/api.py index 8c558e07f..fc355e0bf 100644 --- a/src/docx/api.py +++ b/src/docx/api.py @@ -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 not in (CT.WML_DOCUMENT_MAIN, WML_DOCUMENT_MAIN_WITH_MACROS): + 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