1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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():
text_xml = xml_element("w:t", {}, [xml_text("Hello!")])
run_xml = xml_element("w:r", {}, [text_xml])
paragraph_xml = xml_element("w:p", {}, [run_xml])
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([documents.run([documents.text("Hello!")])])]),
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_footnotes_of_document_are_read():
notes = [documents.note("footnote", "4", [documents.paragraph([])])]
body_xml = xml_element("w:body")
document_xml = xml_element("w:document", {}, [body_xml])
document = _read_and_get_document_xml_element(document_xml, notes=notes)
footnote = document.notes.find_note("footnote", "4")
assert_equal("4", footnote.note_id)
assert isinstance(footnote.body[0], documents.Paragraph)
def _read_and_get_document_xml_element(*args, **kwargs):
body_reader = body_xml.reader()
result = read_document_xml_element(*args, body_reader=body_reader, **kwargs)
assert_equal([], result.messages)
return result.value
|