From aaf459be8337374fe7a78c132164464992bea71a Mon Sep 17 00:00:00 2001 From: Dylan Date: Mon, 13 May 2019 15:41:35 +0200 Subject: [PATCH 1/2] add '.docm' compatibility --- docx/__init__.py | 2 ++ docx/api.py | 4 ++-- docx/opc/constants.py | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docx/__init__.py b/docx/__init__.py index 33e15e31a..8828bb375 100644 --- a/docx/__init__.py +++ b/docx/__init__.py @@ -33,6 +33,8 @@ def part_class_selector(content_type, reltype): PartFactory.part_type_for[CT.WML_NUMBERING] = NumberingPart PartFactory.part_type_for[CT.WML_SETTINGS] = SettingsPart PartFactory.part_type_for[CT.WML_STYLES] = StylesPart +PartFactory.part_type_for[CT.WML_DOCUMENT_MACRO_ENABLED_MAIN] = DocumentPart + del ( CT, diff --git a/docx/api.py b/docx/api.py index 9a2215d7a..f679ed7c1 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 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 @@ -34,4 +34,4 @@ def _default_docx_path(): Return the path to the built-in default .docx package. """ _thisdir = os.path.split(__file__)[0] - return os.path.join(_thisdir, 'templates', 'default-docx-template') + return os.path.join(_thisdir, 'templates', 'default.docx') diff --git a/docx/opc/constants.py b/docx/opc/constants.py index b90aa394a..b1bbcc521 100644 --- a/docx/opc/constants.py +++ b/docx/opc/constants.py @@ -280,6 +280,7 @@ 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 1d2ed37beb448684635e26fd0684e8dcfd397989 Mon Sep 17 00:00:00 2001 From: Dylan Date: Mon, 13 May 2019 15:44:46 +0200 Subject: [PATCH 2/2] correct api.py --- docx/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docx/api.py b/docx/api.py index f679ed7c1..f42f050a8 100644 --- a/docx/api.py +++ b/docx/api.py @@ -34,4 +34,4 @@ def _default_docx_path(): Return the path to the built-in default .docx package. """ _thisdir = os.path.split(__file__)[0] - return os.path.join(_thisdir, 'templates', 'default.docx') + return os.path.join(_thisdir, 'templates', 'default-docx-template')