Пример #1
0
def children_are_recursively_converted_to_text():
    element = documents.document([
        documents.paragraph(
            [documents.text("Hello "),
             documents.text("world.")], {})
    ])

    result = extract_raw_text_from_element(element)

    assert_equal("Hello world.\n\n", result)
Пример #2
0
def main_document_is_found_using_package_relationships():
    fileobj = _create_zip({
        "word/document2.xml":
        textwrap.dedent("""\
            <?xml version="1.0" encoding="utf-8" ?>
            <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
                <w:body>
                    <w:p>
                        <w:r>
                            <w:t>Hello.</w:t>
                        </w:r>
                    </w:p>
                </w:body>
            </w:document>
        """),
        "_rels/.rels":
        textwrap.dedent("""\
            <?xml version="1.0" encoding="utf-8"?>
            <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
                <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="/word/document2.xml" Id="rId1"/>
            </Relationships>
        """),
    })
    result = docx.read(fileobj=fileobj)
    expected_document = documents.document(
        [documents.paragraph([documents.run([documents.text("Hello.")])])])
    assert_equal(expected_document, result.value)
Пример #3
0
def main_document_is_found_using_package_relationships():
    fileobj = _create_zip({
        "word/document2.xml": textwrap.dedent("""\
            <?xml version="1.0" encoding="utf-8" ?>
            <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
                <w:body>
                    <w:p>
                        <w:r>
                            <w:t>Hello.</w:t>
                        </w:r>
                    </w:p>
                </w:body>
            </w:document>
        """),
        "_rels/.rels": textwrap.dedent("""\
            <?xml version="1.0" encoding="utf-8"?>
            <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
                <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="/word/document2.xml" Id="rId1"/>
            </Relationships>
        """),
    })
    result = docx.read(fileobj=fileobj)
    expected_document = documents.document([
        documents.paragraph([
            documents.run([
                documents.text("Hello.")
            ])
        ])
    ])
    assert_equal(expected_document, result.value)
Пример #4
0
def subscript_runs_are_wrapped_in_sub_tags():
    result = convert_document_element_to_html(
        documents.run(
            children=[documents.text("Hello")],
            vertical_alignment=documents.VerticalAlignment.subscript,
        ), )
    assert_equal("<sub>Hello</sub>", result.value)
Пример #5
0
def style_mapping_for_underline_runs_does_not_close_parent_elements():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")],
                      is_underline=True,
                      is_bold=True),
        style_map=[_style_mapping("u => em")])
    assert_equal("<strong><em>Hello</em></strong>", result.value)
Пример #6
0
 def sdt_is_read_using_sdt_content(self):
     element = xml_element("w:sdt", {}, [
         xml_element("w:sdtContent", {}, [
             xml_element("w:t", {}, [xml_text("Blackdown")]),
         ]),
     ])
     result = _read_and_get_document_xml_element(element)
     assert_equal(documents.text("Blackdown"), result)
Пример #7
0
def sdt_is_read_using_sdt_content():
    element = xml_element("w:sdt", {}, [
        xml_element("w:sdtContent", {}, [
            xml_element("w:t", {}, [xml_text("Blackdown")]),
        ]),
    ])
    result = _read_and_get_document_xml_element(element)
    assert_equal(documents.text("Blackdown"), result)
Пример #8
0
def strikethrough_runs_can_be_configured_with_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_strikethrough=True),
        style_map=[
            _style_mapping("strike => del")
        ]
    )
    assert_equal("<del>Hello</del>", result.value)
def underline_runs_can_be_mapped_using_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_underline=True),
        style_map=[
            _style_mapping("u => em")
        ]
    )
    assert_equal("<em>Hello</em>", result.value)
Пример #10
0
def style_mapping_for_underline_runs_does_not_close_parent_elements():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_underline=True, is_bold=True),
        style_map=[
            _style_mapping("u => em")
        ]
    )
    assert_equal("<strong><em>Hello</em></strong>", result.value)
Пример #11
0
def strikethrough_runs_can_be_configured_with_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_strikethrough=True),
        style_map=[
            _style_mapping("strike => del")
        ]
    )
    assert_equal("<del>Hello</del>", result.value)
Пример #12
0
def small_caps_runs_can_be_mapped_using_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_small_caps=True),
        style_map=[
            _style_mapping("small-caps => span")
        ]
    )
    assert_equal("<span>Hello</span>", result.value)
Пример #13
0
def subscript_runs_are_wrapped_in_sub_tags():
    result = convert_document_element_to_html(
        documents.run(
            children=[documents.text("Hello")],
            vertical_alignment=documents.VerticalAlignment.subscript,
        ),
    )
    assert_equal("<sub>Hello</sub>", result.value)
Пример #14
0
def small_caps_runs_can_be_mapped_using_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_small_caps=True),
        style_map=[
            _style_mapping("small-caps => span")
        ]
    )
    assert_equal("<span>Hello</span>", result.value)
Пример #15
0
def underline_runs_can_be_mapped_using_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_underline=True),
        style_map=[
            _style_mapping("u => em")
        ]
    )
    assert_equal("<em>Hello</em>", result.value)
Пример #16
0
 def can_read_document_with_single_paragraph_with_single_run_of_text(self):
     with open(test_path("single-paragraph.docx"), "rb") as fileobj:
         result = docx.read(fileobj=fileobj)
         expected_document = documents.document([
             documents.paragraph([
                 documents.run([documents.text("Walking on imported air")])
             ])
         ])
         assert_equal(expected_document, result.value)
Пример #17
0
 def can_read_document_with_single_paragraph_with_single_run_of_text(self):
     with open(test_path("single-paragraph.docx"), "rb") as fileobj:
         result = docx.read(fileobj=fileobj)
         expected_document = documents.document([
             documents.paragraph([
                 documents.run([
                     documents.text("Walking on imported air")
                 ])
             ])
         ])
         assert_equal(expected_document, result.value)
Пример #18
0
def italic_runs_are_wrapped_in_emphasis_tags_by_default():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_italic=True),
    )
    assert_equal("<em>Hello</em>", result.value)
Пример #19
0
def bold_runs_can_be_configured_with_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_bold=True),
        style_map=[_style_mapping("b => em")]
    )
    assert_equal("<em>Hello</em>", result.value)
Пример #20
0
def italic_runs_can_be_configured_with_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_italic=True),
        style_map=[_style_mapping("i => strong")])
    assert_equal("<strong>Hello</strong>", result.value)
Пример #21
0
def _run_with_text(text):
    return documents.run(children=[documents.text(text)])
Пример #22
0
def soft_hyphen_element_is_read_as_soft_hyphen_character():
    element = xml_element("w:softHyphen")
    tab = _read_and_get_document_xml_element(element)
    assert_equal(documents.text(u"\u00ad"), tab)
Пример #23
0
 def includes_children(self):
     children = [documents.text("child 1"), documents.text("child 2")]
     element = documents.paragraph(children=children)
     assert_equal(children, get_descendants(element))
Пример #24
0
 def includes_indirect_descendants(self):
     grandchild = documents.text("grandchild")
     child = documents.run(children=[grandchild])
     element = documents.paragraph(children=[child])
     assert_equal([grandchild, child], get_descendants(element))
Пример #25
0
def underline_runs_can_be_wrapped_in_tags():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_underline=True),
        convert_underline=mammoth.underline.element("u"))
    assert_equal("<u>Hello</u>", result.value)
Пример #26
0
def underline_runs_are_ignored_by_default():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_underline=True), )
    assert_equal("Hello", result.value)
Пример #27
0
def italic_runs_are_wrapped_in_emphasis_tags():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_italic=True), )
    assert_equal("<em>Hello</em>", result.value)
Пример #28
0
def underline_runs_can_be_wrapped_in_tags_using_convert_underline_argument():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_underline=True),
        convert_underline=mammoth.underline.element("u")
    )
    assert_equal("<u>Hello</u>", result.value)
Пример #29
0
def no_break_hyphen_element_is_read_as_non_breaking_hyphen_character():
    element = xml_element("w:noBreakHyphen")
    tab = _read_and_get_document_xml_element(element)
    assert_equal(documents.text(unichr(0x2011)), tab)
Пример #30
0
def bold_runs_can_be_configured_with_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_bold=True),
        style_map=[_style_mapping("b => em")])
    assert_equal("<em>Hello</em>", result.value)
Пример #31
0
def italic_runs_can_be_configured_with_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_italic=True),
        style_map=[_style_mapping("i => strong")]
    )
    assert_equal("<strong>Hello</strong>", result.value)
Пример #32
0
def underline_runs_are_ignored_by_default():
    result = convert_document_element_to_html(documents.run(children=[documents.text("Hello")], is_underline=True))
    assert_equal("Hello", result.value)
Пример #33
0
def _run_with_text(text):
    return documents.run(children=[documents.text(text)])
Пример #34
0
def strikethrough_runs_are_wrapped_in_s_elements_by_default():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_strikethrough=True),
    )
    assert_equal("<s>Hello</s>", result.value)
Пример #35
0
 def includes_children(self):
     children = [documents.text("child 1"), documents.text("child 2")]
     element = documents.paragraph(children=children)
     assert_equal(children, get_descendants(element))
Пример #36
0
def small_caps_runs_are_ignored_by_default():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_small_caps=True),
    )
    assert_equal("Hello", result.value)
Пример #37
0
 def includes_indirect_descendants(self):
     grandchild = documents.text("grandchild")
     child = documents.run(children=[grandchild])
     element = documents.paragraph(children=[child])
     assert_equal([grandchild, child], get_descendants(element))
Пример #38
0
def no_break_hyphen_element_is_read_as_non_breaking_hyphen_character():
    element = xml_element("w:noBreakHyphen")
    tab = _read_and_get_document_xml_element(element)
    assert_equal(documents.text(unichr(0x2011)), tab)
Пример #39
0
def bold_runs_are_wrapped_in_strong_tags_by_default():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_bold=True),
    )
    assert_equal("<strong>Hello</strong>", result.value)
Пример #40
0
def bold_runs_are_wrapped_in_strong_tags():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_bold=True), )
    assert_equal("<strong>Hello</strong>", result.value)
Пример #41
0
def strikethrough_runs_are_wrapped_in_s_elements_by_default():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")],
                      is_strikethrough=True), )
    assert_equal("<s>Hello</s>", result.value)