diff options
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/docx/__init__.py | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py index 9a00dd5..02dad15 100644 --- a/mammoth/docx/__init__.py +++ b/mammoth/docx/__init__.py @@ -38,11 +38,8 @@ def read(fileobj): with _open_entry(zip_file, "word/styles.xml") as styles_fileobj: styles = read_styles_xml_element(_parse_docx_xml(styles_fileobj)) - if _has_entry(zip_file, "word/footnotes.xml"): - with _open_entry(zip_file, "word/footnotes.xml") as footnotes_fileobj: - footnote_elements = read_footnotes_xml_element(_parse_docx_xml(footnotes_fileobj)) - else: - footnote_elements = [] + footnote_elements = _try_read_entry_or_default( + zip_file, "word/footnotes.xml", read_footnotes_xml_element, default=[]) with _open_entry(zip_file, "word/document.xml") as document_fileobj: document_xml = _parse_docx_xml(document_fileobj) @@ -70,6 +67,14 @@ def _open_entry(zip_file, name): entry.close() +def _try_read_entry_or_default(zip_file, name, reader, default): + if _has_entry(zip_file, name): + with _open_entry(zip_file, name) as fileobj: + return reader(_parse_docx_xml(fileobj)) + else: + return default + + def _has_entry(zip_file, name): try: zip_file.getinfo(name) |
