Пример #1
0
 def can_read_linked_pictures(self, mocks):
     drawing_element = _create_inline_image(
         blip=_linked_blip("rId5"),
         description="It's a hat",
     )
     
     image_bytes = b"Not an image at all!"
     
     relationships = Relationships({
         "rId5": Relationship(target="file:///media/hat.png")
     })
     
     files = mocks.mock()
     funk.allows(files).verify("file:///media/hat.png")
     funk.allows(files).open("file:///media/hat.png").returns(io.BytesIO(image_bytes))
     
     content_types = mocks.mock()
     funk.allows(content_types).find_content_type("file:///media/hat.png").returns("image/png")
     
     image = _read_and_get_document_xml_element(
         drawing_element,
         content_types=content_types,
         relationships=relationships,
         files=files,
     )
     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())
Пример #2
0
 def can_read_imagedata_elements_with_rid_attribute(self, mocks):
     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 = mocks.mock()
     funk.allows(docx_file).open("word/media/hat.png").returns(io.BytesIO(image_bytes))
     
     content_types = mocks.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())
Пример #3
0
 def warning_if_unsupported_image_type(self, mocks):
     drawing_element = _create_inline_image(
         blip=_embedded_blip("rId5"),
         description="It's a hat",
     )
     
     image_bytes = b"Not an image at all!"
     
     relationships = Relationships({
         "rId5": Relationship(target="media/hat.emf")
     })
     
     docx_file = mocks.mock()
     funk.allows(docx_file).open("word/media/hat.emf").returns(io.BytesIO(image_bytes))
     
     content_types = mocks.mock()
     funk.allows(content_types).find_content_type("word/media/hat.emf").returns("image/x-emf")
     
     result = _read_document_xml_element(
         drawing_element,
         content_types=content_types,
         relationships=relationships,
         docx_file=docx_file,
     )
     assert_equal("image/x-emf", result.value.content_type)
     expected_warning = results.warning("Image of type image/x-emf is unlikely to display in web browsers")
     assert_equal([expected_warning], result.messages)
Пример #4
0
def _image_relationship(relationship_id, target):
    return Relationship(
        relationship_id=relationship_id,
        target=target,
        type=
        "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
    )
    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())
 def hyperlink_is_read_if_it_has_a_relationship_id(self):
     relationships = Relationships(
         {"r42": Relationship(target="http://example.com")})
     run_element = xml_element("w:r")
     element = xml_element("w:hyperlink", {"r:id": "r42"}, [run_element])
     assert_equal(
         documents.hyperlink("http://example.com", [documents.run([])]),
         _read_and_get_document_xml_element(element,
                                            relationships=relationships))
Пример #7
0
 def existing_fragment_is_replaced_when_anchor_is_set_on_external_link(self):
     relationships = Relationships({
         "r42": Relationship(target="http://example.com/#previous")
     })
     run_element = xml_element("w:r")
     element = xml_element("w:hyperlink", {"r:id": "r42", "w:anchor": "fragment"}, [run_element])
     assert_equal(
         documents.hyperlink(href="http://example.com/#fragment", children=[documents.run([])]),
         _read_and_get_document_xml_element(element, relationships=relationships)
     )
Пример #8
0
 def hyperlink_is_read_as_external_hyperlink_if_it_has_a_relationship_id_and_an_anchor(self):
     relationships = Relationships({
         "r42": Relationship(target="http://example.com/")
     })
     run_element = xml_element("w:r")
     element = xml_element("w:hyperlink", {"r:id": "r42", "w:anchor": "fragment"}, [run_element])
     assert_equal(
         documents.hyperlink(href="http://example.com/#fragment", children=[documents.run([])]),
         _read_and_get_document_xml_element(element, relationships=relationships)
     )
def target_is_read_from_relationship_element():
    element = xml_element("relationships:Relationships", {}, [
        xml_element("relationships:Relationship", {
            "Id": "rId8",
            "Type": "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
            "Target": "http://example.com",
        })
    ])
    relationships = read_relationships_xml_element(element)
    assert_equal(
        Relationship(target="http://example.com"),
        relationships["rId8"],
    )
Пример #10
0
 def _read_embedded_image(self, element):
     relationships = Relationships({
         self.IMAGE_RELATIONSHIP_ID: Relationship(target="media/hat.png")
     })
     
     mocks = funk.Mocks()
     docx_file = mocks.mock()
     funk.allows(docx_file).open("word/media/hat.png").returns(io.BytesIO(self.IMAGE_BYTES))
     
     content_types = mocks.mock()
     funk.allows(content_types).find_content_type("word/media/hat.png").returns("image/png")
     
     return _read_and_get_document_xml_element(
         element,
         content_types=content_types,
         relationships=relationships,
         docx_file=docx_file,
     )