aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-12-01 21:25:48 +0000
committerMichael Williamson <mike@zwobble.org>2013-12-01 21:25:48 +0000
commit4fd08468ad3823d09041a0ffa33aba2dd509e192 (patch)
treeb8420481e1307f6539c94c27c54b6f082bfb4900
parent9b479955a7c5b0547db8b274d2dfd6d0d6152194 (diff)
Add support for reading anchored images
-rw-r--r--mammoth/docx/document_xml.py2
-rw-r--r--tests/docx/document_xml_tests.py60
2 files changed, 53 insertions, 9 deletions
diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py
index f8f1656..9c895d4 100644
--- a/mammoth/docx/document_xml.py
+++ b/mammoth/docx/document_xml.py
@@ -29,6 +29,7 @@ def _create_reader(numbering, content_types, relationships, docx_file):
def handler(name):
def add(func):
_handlers[name] = func
+ return func
return add
@@ -106,6 +107,7 @@ def _create_reader(numbering, content_types, relationships, docx_file):
return _read_xml_elements(element.children)
@handler("wp:inline")
+ @handler("wp:anchor")
def inline(element):
alt_text = element.find_child_or_null("wp:docPr").attributes.get("descr")
blips = element.find_children("a:graphic") \
diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py
index 36c663b..9badbac 100644
--- a/tests/docx/document_xml_tests.py
+++ b/tests/docx/document_xml_tests.py
@@ -160,6 +160,38 @@ class ReadXmlElementTests(object):
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_anchored_pictures(self, context):
+ drawing_element = _create_anchored_image(
+ relationship_id="rId5",
+ description="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(
+ drawing_element,
+ content_types=content_types,
+ relationships=relationships,
+ docx_file=docx_file,
+ )[0]
+ 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
def ignored_elements_are_ignored_without_message(self):
@@ -214,16 +246,26 @@ def _text_element(value):
def _create_inline_image(description, relationship_id):
return xml_element("w:drawing", {}, [
- xml_element("wp:inline", {}, [
- xml_element("wp:docPr", {"descr": description}),
- xml_element("a:graphic", {}, [
- xml_element("a:graphicData", {}, [
- xml_element("pic:pic", {}, [
- xml_element("pic:blipFill", {}, [
- xml_element("a:blip", {"r:embed": relationship_id})
- ])
+ xml_element("wp:inline", {}, _create_image_elements(description, relationship_id))
+ ])
+
+
+def _create_anchored_image(description, relationship_id):
+ return xml_element("w:drawing", {}, [
+ xml_element("wp:anchor", {}, _create_image_elements(description, relationship_id))
+ ])
+
+
+def _create_image_elements(description, relationship_id):
+ return [
+ xml_element("wp:docPr", {"descr": description}),
+ xml_element("a:graphic", {}, [
+ xml_element("a:graphicData", {}, [
+ xml_element("pic:pic", {}, [
+ xml_element("pic:blipFill", {}, [
+ xml_element("a:blip", {"r:embed": relationship_id})
])
])
])
])
- ])
+ ]