Skip to content

Commit

Permalink
Improve error message when failing to find the body element in a docu…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
mwilliamson committed Feb 18, 2024
1 parent 0204b94 commit c210c30
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Support attributes in HTML paths in style mappings.

* Improve error message when failing to find the body element in a document.

* Drop support for Python 2.7, Python 3.5 and Python 3.6.

# 1.6.0
Expand Down
8 changes: 6 additions & 2 deletions mammoth/docx/document_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ def read_document_xml_element(
body_reader,
notes=None,
comments=None):

if notes is None:
notes = []
if comments is None:
comments = []

body_element = element.find_child("w:body")

if body_element is None:
raise ValueError("Could not find the body element: are you sure this is a docx file?")

return body_reader.read_all(body_element.children) \
.map(lambda children: documents.document(
children,
Expand Down
25 changes: 25 additions & 0 deletions tests/docx/document_xml_tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import pytest

from mammoth import documents
from mammoth.docx.xmlparser import element as xml_element, text as xml_text
from mammoth.docx.document_xml import read_document_xml_element
from mammoth.docx import body_xml
from ..testing import assert_equal


def test_when_body_element_is_present_then_body_is_read():
paragraph_xml = xml_element("w:p", {}, [])
body_xml = xml_element("w:body", {}, [paragraph_xml])
document_xml = xml_element("w:document", {}, [body_xml])

document = _read_and_get_document_xml_element(document_xml)

assert_equal(
documents.document([documents.paragraph([])]),
document
)


def test_when_body_element_is_not_present_then_error_is_raised():
paragraph_xml = xml_element("w:p", {}, [])
body_xml = xml_element("w:body2", {}, [paragraph_xml])
document_xml = xml_element("w:document", {}, [body_xml])

error = pytest.raises(ValueError, lambda: _read_and_get_document_xml_element(document_xml))

assert_equal(str(error.value), "Could not find the body element: are you sure this is a docx file?")


def test_can_read_text_within_document():
element = _document_element_with_text("Hello!")
assert_equal(
Expand Down

0 comments on commit c210c30

Please sign in to comment.