From c09da22afaebaed2f0a3139de6ba46c8824f179e Mon Sep 17 00:00:00 2001 From: Emanuele Cannizzaro Date: Mon, 2 Sep 2019 22:39:14 +0200 Subject: [PATCH 1/2] dded support for .docm files --- docx/__init__.py | 1 + docx/api.py | 2 +- docx/opc/constants.py | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docx/__init__.py b/docx/__init__.py index 4dae2946b..7f6e6c488 100644 --- a/docx/__init__.py +++ b/docx/__init__.py @@ -28,6 +28,7 @@ def part_class_selector(content_type, reltype): 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_MACRO_ENABLED_MAIN] = 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/docx/api.py b/docx/api.py index 63e18c406..8df634dc5 100644 --- a/docx/api.py +++ b/docx/api.py @@ -23,7 +23,7 @@ def Document(docx=None): """ docx = _default_docx_path() if docx is None else docx document_part = Package.open(docx).main_document_part - if document_part.content_type != CT.WML_DOCUMENT_MAIN: + if (document_part.content_type != CT.WML_DOCUMENT_MAIN) and (document_part.content_type != CT.WML_DOCUMENT_MACRO_ENABLED_MAIN): 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/docx/opc/constants.py b/docx/opc/constants.py index b90aa394a..e15c21f8b 100644 --- a/docx/opc/constants.py +++ b/docx/opc/constants.py @@ -280,6 +280,9 @@ class CONTENT_TYPE(object): 'application/vnd.openxmlformats-officedocument.wordprocessingml.docu' 'ment.main+xml' ) + WML_DOCUMENT_MACRO_ENABLED_MAIN = ( + 'application/vnd.ms-word.document.macroEnabled.main+xml' + ) WML_ENDNOTES = ( 'application/vnd.openxmlformats-officedocument.wordprocessingml.endn' 'otes+xml' From 98f519169db7edcea68552fcf52106d59de2937c Mon Sep 17 00:00:00 2001 From: Emanuele Cannizzaro Date: Mon, 2 Sep 2019 22:51:16 +0200 Subject: [PATCH 2/2] Small tidying up to the macro-enabled file --- docx/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docx/api.py b/docx/api.py index 8df634dc5..adf44b180 100644 --- a/docx/api.py +++ b/docx/api.py @@ -23,7 +23,7 @@ def Document(docx=None): """ docx = _default_docx_path() if docx is None else docx document_part = Package.open(docx).main_document_part - if (document_part.content_type != CT.WML_DOCUMENT_MAIN) and (document_part.content_type != CT.WML_DOCUMENT_MACRO_ENABLED_MAIN): + if document_part.content_type not in [CT.WML_DOCUMENT_MAIN, CT.WML_DOCUMENT_MACRO_ENABLED_MAIN]: tmpl = "file '%s' is not a Word file, content type is '%s'" raise ValueError(tmpl % (docx, document_part.content_type)) return document_part.document