diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-12-06 14:49:08 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-12-06 14:49:08 +0000 |
| commit | 57a0d70f895d6e39ddc2aac46c53c4088f90cc3f (patch) | |
| tree | 96f80c7e1e98a5eb4864e2258f9b1b38fcb46538 | |
| parent | a0ffb89a9c3abf5605f5497034d29f6286246b53 (diff) | |
Read v:imagedata with r:id attribute
| -rw-r--r-- | NEWS | 2 | ||||
| -rw-r--r-- | mammoth/docx/body_xml.py | 10 | ||||
| -rw-r--r-- | tests/docx/body_xml_tests.py | 30 |
3 files changed, 41 insertions, 1 deletions
@@ -2,6 +2,8 @@ * Remove duplicate messages from results. +* Read v:imagedata with r:id attribute. + * Read children of v:roundrect. * Ignore office-word:wrap, v:shadow and v:shapetype. diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index 42c79a2..23ae944 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -199,8 +199,11 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f return _combine_results(map(lambda blip: _read_blip(blip, alt_text), blips)) def _read_blip(element, alt_text): + return _read_image(lambda: _read_blip_image(element), alt_text) + + def _read_image(find_image, alt_text): try: - image_path, open_image = _read_blip_image(element) + image_path, open_image = find_image() except InvalidFileReferenceError as error: return _empty_result_with_messages([results.warning(str(error))]) @@ -246,6 +249,10 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f return image_path, open_image + def read_imagedata(element): + title = element.attributes.get("o:title") + return _read_image(lambda: _read_embedded_blip_image(element.attributes["r:id"]), title) + def note_reference_reader(note_type): def note_reference(element): return _success(documents.note_reference(note_type, element.attributes["w:id"])) @@ -276,6 +283,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f "w:br": br, "wp:inline": inline, "wp:anchor": inline, + "v:imagedata": read_imagedata, "w:footnoteReference": note_reference_reader("footnote"), "w:endnoteReference": note_reference_reader("endnote"), "mc:AlternateContent": alternate_content, diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index da662d8..d0288bd 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -297,6 +297,36 @@ class ReadXmlElementTests(object): @istest @funk.with_context + def can_read_imagedata_elements_with_rid_attribute(self, context): + imagedata_element = xml_element("v:imagedata", {"r:id": "rId5", "o:title": "It's a hat"}) + + image_bytes = b"Not an image at all!" + + relationships = Relationships({ + "rId5": Relationship(target="media/hat.png") + }) + + docx_file = context.mock() + funk.allows(docx_file).open("word/media/hat.png").returns(io.BytesIO(image_bytes)) + + content_types = context.mock() + funk.allows(content_types).find_content_type("word/media/hat.png").returns("image/png") + + image = _read_and_get_document_xml_element( + imagedata_element, + content_types=content_types, + relationships=relationships, + docx_file=docx_file, + ) + assert_equal(documents.Image, type(image)) + assert_equal("It's a hat", image.alt_text) + assert_equal("image/png", image.content_type) + with image.open() as image_file: + assert_equal(image_bytes, image_file.read()) + + + @istest + @funk.with_context def can_read_inline_pictures(self, context): drawing_element = _create_inline_image( blip=_embedded_blip("rId5"), |
