diff options
Diffstat (limited to 'mammoth/docx')
| -rw-r--r-- | mammoth/docx/__init__.py | 30 | ||||
| -rw-r--r-- | mammoth/docx/xmlparser.py | 12 |
2 files changed, 29 insertions, 13 deletions
diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py index a0576cf..58bfa50 100644 --- a/mammoth/docx/__init__.py +++ b/mammoth/docx/__init__.py @@ -12,7 +12,7 @@ from .styles_xml import read_styles_xml_element, Styles from .notes_xml import read_endnotes_xml_element, read_footnotes_xml_element from .comments_xml import read_comments_xml_element from .files import Files -from . import body_xml, office_xml +from . import body_xml, office_xml, xmlparser from ..zips import open_zip @@ -33,7 +33,7 @@ def read(fileobj, external_file_access=False): _read_notes(read_part_with_body, part_paths), _read_comments(read_part_with_body, part_paths), ]).bind(lambda referents: - _read_document(zip_file, read_part_with_body, notes=referents[0], comments=referents[1], part_paths=part_paths) + _read_document(read_part_with_body, notes=referents[0], comments=referents[1], part_paths=part_paths) ) @@ -124,15 +124,18 @@ def _read_comments(read_part_with_body, part_paths): ) -def _read_document(zip_file, read_part_with_body, notes, comments, part_paths): - return read_part_with_body( - part_paths.main_document, - partial( - read_document_xml_element, - notes=notes, - comments=comments, - ), - ) +def _read_document(read_part_with_body, notes, comments, part_paths): + try: + return read_part_with_body( + part_paths.main_document, + partial( + read_document_xml_element, + notes=notes, + comments=comments, + ), + ) + except xmlparser.NoRootElementError: + raise IOError("Main document part is missing root element. Are you sure this is a valid .docx file?") def _part_with_body_reader(document_path, zip_file, part_paths, external_file_access): @@ -198,7 +201,10 @@ def _read_relationships(zip_file, name): def _try_read_entry_or_default(zip_file, name, reader, default): if zip_file.exists(name): - return _read_entry(zip_file, name, reader) + try: + return _read_entry(zip_file, name, reader) + except xmlparser.NoRootElementError: + return default else: return default diff --git a/mammoth/docx/xmlparser.py b/mammoth/docx/xmlparser.py index 1ba415d..d78e610 100644 --- a/mammoth/docx/xmlparser.py +++ b/mammoth/docx/xmlparser.py @@ -1,4 +1,5 @@ import xml.dom.minidom +import xml.parsers.expat.errors import cobble @@ -74,6 +75,9 @@ XmlElement.node_type = node_types.element XmlText.node_type = node_types.text +class NoRootElementError(Exception): + pass + def parse_xml(fileobj, namespace_mapping=None): if namespace_mapping is None: @@ -81,7 +85,13 @@ def parse_xml(fileobj, namespace_mapping=None): else: namespace_prefixes = dict((uri, prefix) for prefix, uri in namespace_mapping) - document = xml.dom.minidom.parse(fileobj) + try: + document = xml.dom.minidom.parse(fileobj) + except xml.parsers.expat.ExpatError as error: + if error.code == xml.parsers.expat.errors.codes[xml.parsers.expat.errors.XML_ERROR_NO_ELEMENTS]: + raise NoRootElementError() + else: + raise def convert_node(node): if node.nodeType == xml.dom.Node.ELEMENT_NODE: |
