Exemple #1
0
 def warning_on_break_types_that_arent_recognised(self):
     break_element = xml_element("w:br", {"w:type": "unknownBreakType"}, [])
     result = _read_document_xml_element(break_element)
     expected_warning = results.warning(
         "Unsupported break type: unknownBreakType")
     assert_equal([expected_warning], result.messages)
     assert_equal(None, result.value)
    def when_imagedata_element_has_no_relationship_id_then_it_is_ignored_with_warning(self):
        imagedata_element = xml_element("v:imagedata")

        result = _read_document_xml_element(imagedata_element)
        expected_warning = results.warning("A v:imagedata element without a relationship ID was ignored")
        assert_equal([expected_warning], result.messages)
        assert_equal(None, result.value)
Exemple #3
0
 def warning_if_non_cell_in_table_row(self):
     element = xml_element("w:tbl", {}, [w_tr(xml_element("w:p"))])
     result = _read_document_xml_element(element)
     expected_warning = results.warning(
         "unexpected non-cell element in table row, cell merging may be incorrect"
     )
     assert_equal([expected_warning], result.messages)
Exemple #4
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)
Exemple #5
0
    def when_imagedata_element_has_no_relationship_id_then_it_is_ignored_with_warning(self):
        imagedata_element = xml_element("v:imagedata")

        result = _read_document_xml_element(imagedata_element)
        expected_warning = results.warning("A v:imagedata element without a relationship ID was ignored")
        assert_equal([expected_warning], result.messages)
        assert_equal(None, result.value)
 def warning_if_unsupported_image_type(self, context):
     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 = 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 warning_is_emitted_if_paragraph_style_is_unrecognised():
    result = convert_document_element_to_html(
        documents.paragraph(style_name="TipsParagraph", children=[
            _run_with_text("Tip")
        ]),
    )
    assert_equal([results.warning("Unrecognised paragraph style: TipsParagraph")], result.messages)
    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 warn_if_images_stored_outside_of_document_are_specified_when_passing_fileobj_without_name():
    fileobj = io.BytesIO()
    with open(test_path("external-picture.docx"), "rb") as source_fileobj:
        shutil.copyfileobj(source_fileobj, fileobj)
    
    result = mammoth.convert_to_html(fileobj=fileobj)
    assert_equal("", result.value)
    assert_equal([results.warning("could not find external image 'tiny-picture.png', fileobj has no name")], result.messages)
def warning_if_style_mapping_is_not_understood():
    style_map = """
!!!!
p => h1"""
    with open(test_path("single-paragraph.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj, style_map=style_map)
        assert_equal("<h1>Walking on imported air</h1>", result.value)
        warning = "Did not understand this style mapping, so ignored it: !!!!"
        assert_equal([results.warning(warning)], result.messages)
def warning_if_style_mapping_is_not_understood():
    style_map = """
!!!!
p => h1"""
    with open(test_path("single-paragraph.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj, style_map=style_map)
        assert_equal("<h1>Walking on imported air</h1>", result.value)
        warning = "Did not understand this style mapping, so ignored it: !!!!"
        assert_equal([results.warning(warning)], result.messages)
def warning_is_emitted_if_paragraph_style_is_unrecognised():
    result = convert_document_element_to_html(
        documents.paragraph(style_id="Heading1",
                            style_name="Heading 1",
                            children=[_run_with_text("Tip")]), )
    assert_equal([
        results.warning(
            "Unrecognised paragraph style: Heading 1 (Style ID: Heading1)")
    ], result.messages)
    def warning_is_emitted_when_table_style_cannot_be_found(self):
        style_xml = xml_element("w:tblStyle", {"w:val": "TableNormal"})
        properties_xml = xml_element("w:tblPr", {}, [style_xml])
        table_xml = xml_element("w:tbl", {}, [properties_xml])

        result = _read_document_xml_element(table_xml, styles=Styles.EMPTY)
        table = result.value
        assert_equal("TableNormal", table.style_id)
        assert_equal(None, table.style_name)
        assert_equal([results.warning("Table style with ID TableNormal was referenced but not defined in the document")], result.messages)
    def warning_is_emitted_when_paragraph_style_cannot_be_found(self):
        style_xml = xml_element("w:pStyle", {"w:val": "Heading1"})
        properties_xml = xml_element("w:pPr", {}, [style_xml])
        paragraph_xml = xml_element("w:p", {}, [properties_xml])

        result = _read_document_xml_element(paragraph_xml, styles=Styles.EMPTY)
        paragraph = result.value
        assert_equal("Heading1", paragraph.style_id)
        assert_equal(None, paragraph.style_name)
        assert_equal([results.warning("Paragraph style with ID Heading1 was referenced but not defined in the document")], result.messages)
Exemple #15
0
    def warning_is_emitted_when_table_style_cannot_be_found(self):
        style_xml = xml_element("w:tblStyle", {"w:val": "TableNormal"})
        properties_xml = xml_element("w:tblPr", {}, [style_xml])
        table_xml = xml_element("w:tbl", {}, [properties_xml])

        result = _read_document_xml_element(table_xml, styles=Styles.EMPTY)
        table = result.value
        assert_equal("TableNormal", table.style_id)
        assert_equal(None, table.style_name)
        assert_equal([results.warning("Table style with ID TableNormal was referenced but not defined in the document")], result.messages)
Exemple #16
0
    def warning_is_emitted_when_paragraph_style_cannot_be_found(self):
        style_xml = xml_element("w:pStyle", {"w:val": "Heading1"})
        properties_xml = xml_element("w:pPr", {}, [style_xml])
        paragraph_xml = xml_element("w:p", {}, [properties_xml])

        result = _read_document_xml_element(paragraph_xml, styles=Styles.EMPTY)
        paragraph = result.value
        assert_equal("Heading1", paragraph.style_id)
        assert_equal(None, paragraph.style_name)
        assert_equal([results.warning("Paragraph style with ID Heading1 was referenced but not defined in the document")], result.messages)
Exemple #17
0
    def warning_is_emitted_when_run_style_cannot_be_found(self):
        style_xml = xml_element("w:rStyle", {"w:val": "Heading1Char"})
        properties_xml = xml_element("w:rPr", {}, [style_xml])
        run_xml = xml_element("w:r", {}, [properties_xml])

        result = _read_document_xml_element(run_xml, styles=Styles.EMPTY)
        run = result.value
        assert_equal("Heading1Char", run.style_id)
        assert_equal(None, run.style_name)
        assert_equal([results.warning("Run style with ID Heading1Char was referenced but not defined in the document")], result.messages)
    def warning_is_emitted_when_run_style_cannot_be_found(self):
        style_xml = xml_element("w:rStyle", {"w:val": "Heading1Char"})
        properties_xml = xml_element("w:rPr", {}, [style_xml])
        run_xml = xml_element("w:r", {}, [properties_xml])

        result = _read_document_xml_element(run_xml, styles=Styles.EMPTY)
        run = result.value
        assert_equal("Heading1Char", run.style_id)
        assert_equal(None, run.style_name)
        assert_equal([results.warning("Run style with ID Heading1Char was referenced but not defined in the document")], result.messages)
def warn_if_images_stored_outside_of_document_are_specified_when_passing_fileobj_without_name(
):
    fileobj = io.BytesIO()
    with open(test_path("external-picture.docx"), "rb") as source_fileobj:
        shutil.copyfileobj(source_fileobj, fileobj)

    result = mammoth.convert_to_html(fileobj=fileobj)
    assert_equal("", result.value)
    assert_equal([
        results.warning(
            "could not find external image 'tiny-picture.png', fileobj has no name"
        )
    ], result.messages)
 def warning_if_non_cell_in_table_row(self):
     element = xml_element("w:tbl", {}, [w_tr(xml_element("w:p"))])
     result = _read_document_xml_element(element)
     expected_warning = results.warning("unexpected non-cell element in table row, cell merging may be incorrect")
     assert_equal([expected_warning], result.messages)
 def unrecognised_elements_emit_warning(self):
     element = xml_element("w:huh", {}, [])
     result = _read_document_xml_element(element)
     expected_warning = results.warning("An unrecognised element was ignored: w:huh")
     assert_equal([expected_warning], result.messages)
 def warning_on_breaks_that_arent_line_breaks(self):
     break_element = xml_element("w:br", {"w:type": "page"}, [])
     result = _read_document_xml_element(break_element)
     expected_warning = results.warning("Unsupported break type: page")
     assert_equal([expected_warning], result.messages)
     assert_equal(None, result.value)
Exemple #23
0
def unrecognised_elements_emit_warning():
    element = xml_element("w:huh", {}, [])
    result = _read_document_xml_element(element)
    expected_warning = results.warning("An unrecognised element was ignored: w:huh")
    assert_equal([expected_warning], result.messages)
 def warning_on_break_types_that_arent_recognised(self):
     break_element = xml_element("w:br", {"w:type": "unknownBreakType"}, [])
     result = _read_document_xml_element(break_element)
     expected_warning = results.warning("Unsupported break type: unknownBreakType")
     assert_equal([expected_warning], result.messages)
     assert_equal(None, result.value)
 def warning_on_breaks_that_arent_line_breaks(self):
     break_element = xml_element("w:br", {"w:type": "page"}, [])
     result = _read_document_xml_element(break_element)
     expected_warning = results.warning("Unsupported break type: page")
     assert_equal([expected_warning], result.messages)
     assert_equal(None, result.value)