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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
def _run_with_text(text): return documents.run(children=[documents.text(text)])
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)
def includes_children(self): children = [documents.text("child 1"), documents.text("child 2")] element = documents.paragraph(children=children) assert_equal(children, get_descendants(element))
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))
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)