Exemple #1
0
    def can_read_linked_pictures(self, mocks):
        drawing_element = _create_inline_image(
            blip=_linked_blip("rId5"),
            description="It's a hat",
        )

        relationships = Relationships([
            _image_relationship("rId5", "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(self.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(self.IMAGE_BYTES, image_file.read())
Exemple #2
0
    def warning_if_unsupported_image_type(self, mocks):
        drawing_element = _create_inline_image(
            blip=_embedded_blip("rId5"),
            description="It's a hat",
        )

        relationships = Relationships([
            _image_relationship("rId5", "media/hat.emf"),
        ])

        docx_file = mocks.mock()
        funk.allows(docx_file).open("word/media/hat.emf").returns(io.BytesIO(self.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)
 def can_read_anchored_pictures(self, mocks):
     drawing_element = _create_anchored_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.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(
         drawing_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())
 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())
    def warning_if_unsupported_image_type(self, context):
        drawing_element = _create_inline_image(
            relationship_id="rId5",
            description="It's a hat",
        )

        image_bytes = b"Not an image at all!"

        relationships = Relationships(
            {"rId5": Relationship(target="media/hat.emf")})

        docx_file = context.mock()
        funk.allows(docx_file).open("word/media/hat.emf").returns(
            io.BytesIO(image_bytes))

        content_types = context.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[0].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)
 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))
Exemple #7
0
 def existing_fragment_is_replaced_when_anchor_is_set_on_external_link(self):
     relationships = Relationships([
         _hyperlink_relationship("r42", "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)
     )
Exemple #8
0
 def hyperlink_is_read_as_external_hyperlink_if_it_has_a_relationship_id_and_an_anchor(self):
     relationships = Relationships([
         _hyperlink_relationship("r42", "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)
     )
Exemple #9
0
    def _read_embedded_image(self, element):
        relationships = Relationships([
            _image_relationship(self.IMAGE_RELATIONSHIP_ID, "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,
        )