import io import textwrap import zipfile from nose.tools import istest, assert_equal from mammoth import docx, documents from ..testing import test_path @istest class ReadTests(object): @istest def can_read_document_with_single_paragraph_with_single_run_of_text(self): with open(test_path("single-paragraph.docx"), "rb") as fileobj: result = docx.read(fileobj=fileobj) expected_document = documents.document([ documents.paragraph([ documents.run([ documents.text("Walking on imported air") ]) ]) ]) assert_equal(expected_document, result.value) _relationship_namespaces = { "r": "http://schemas.openxmlformats.org/package/2006/relationships", } @istest def when_document_xml_is_not_present_then_document_is_found_using_file_relationships(): fileobj = _create_zip({ "word/document2.xml": textwrap.dedent("""\ Hello. """), "_rels/.rels": textwrap.dedent("""\ """), }) result = docx.read(fileobj=fileobj) expected_document = documents.document([ documents.paragraph([ documents.run([ documents.text("Hello.") ]) ]) ]) assert_equal(expected_document, result.value) def _create_zip(files): fileobj = io.BytesIO() with zipfile.ZipFile(fileobj, "w") as zip_file: for name, contents in files.items(): zip_file.writestr(name, contents) fileobj.seek(0) return fileobj