def can_read_text_within_document(self):
     element = _document_element_with_text("Hello!")
     assert_equal(
         documents.document([
             documents.paragraph(
                 [documents.run([documents.Text("Hello!")])])
         ]), _read_and_get_document_xml_element(element))
Example #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)
def comment_references_are_linked_to_comment_after_main_body():
    reference = documents.comment_reference("4")
    comment = documents.comment(
        comment_id="4",
        body=[_paragraph_with_text("Who's there?")],
        author_name="The Piemaker",
        author_initials="TP",
    )
    document = documents.document(
        [
            documents.paragraph(
                [_run_with_text("Knock knock"),
                 documents.run([reference])])
        ],
        comments=[comment],
    )
    result = convert_document_element_to_html(
        document,
        id_prefix="doc-42-",
        style_map=[_style_mapping("comment-reference => sup")],
    )
    expected_html = (
        '<p>Knock knock<sup><a href="#doc-42-comment-4" id="doc-42-comment-ref-4">[TP1]</a></sup></p>'
        +
        '<dl><dt id="doc-42-comment-4">Comment [TP1]</dt><dd><p>Who\'s there? <a href="#doc-42-comment-ref-4">↑</a></p></dd></dl>'
    )
    assert_equal(expected_html, result.value)
def multiple_paragraphs_are_converted_to_multiple_paragraphs():
    result = convert_document_element_to_html(
        documents.document([
            documents.paragraph(children=[_run_with_text("Hello")]),
            documents.paragraph(children=[_run_with_text("there")]),
        ]))
    assert_equal('<p>Hello</p><p>there</p>', result.value)
def comment_references_are_linked_to_comment_after_main_body():
    reference = documents.comment_reference("4")
    comment = documents.comment(
        comment_id="4",
        body=[_paragraph_with_text("Who's there?")],
        author_name="The Piemaker",
        author_initials="TP",
    )
    document = documents.document(
        [documents.paragraph([
            _run_with_text("Knock knock"),
            documents.run([reference])
        ])],
        comments=[comment],
    )
    result = convert_document_element_to_html(
        document,
        id_prefix="doc-42-",
        style_map=[
            _style_mapping("comment-reference => sup")
        ],
    )
    expected_html = (
        '<p>Knock knock<sup><a href="#doc-42-comment-4" id="doc-42-comment-ref-4">[TP1]</a></sup></p>' +
        '<dl><dt id="doc-42-comment-4">Comment [TP1]</dt><dd><p>Who\'s there? <a href="#doc-42-comment-ref-4">↑</a></p></dd></dl>'
    )
    assert_equal(expected_html, result.value)
Example #6
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)
def multiple_paragraphs_are_converted_to_multiple_paragraphs():
    result = convert_document_element_to_html(
        documents.document([
            documents.paragraph(children=[_run_with_text("Hello")]),
            documents.paragraph(children=[_run_with_text("there")]),
        ])
    )
    assert_equal('<p>Hello</p><p>there</p>', result.value)
def docx_hyperlinks_can_be_collapsed():
    result = convert_document_element_to_html(
        documents.document(children=[
            documents.hyperlink(href="http://example.com",
                                children=[documents.Text("Hello ")]),
            documents.hyperlink(href="http://example.com",
                                children=[documents.Text("world")]),
        ]))
    assert_equal('<a href="http://example.com">Hello world</a>', result.value)
Example #9
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)
Example #10
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)
Example #11
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)
Example #12
0
def footnotes_are_included_after_the_main_body():
    footnote_reference = documents.note_reference("footnote", "4")
    document = documents.document(
        [documents.paragraph([_run_with_text("Knock knock"), documents.run([footnote_reference])])],
        notes=documents.notes([documents.note("footnote", "4", [_paragraph_with_text("Who's there?")])]),
    )
    result = convert_document_element_to_html(document, id_prefix="doc-42")
    expected_html = (
        '<p>Knock knock<sup><a href="#doc-42-footnote-4" id="doc-42-footnote-ref-4">[1]</a></sup></p>'
        + '<ol><li id="doc-42-footnote-4"><p>Who\'s there? <a href="#doc-42-footnote-ref-4">↑</a></p></li></ol>'
    )
    assert_equal(expected_html, result.value)
def comments_are_ignored_by_default():
    reference = documents.comment_reference("4")
    comment = documents.comment(
        comment_id="4",
        body=[_paragraph_with_text("Who's there?")],
    )
    document = documents.document(
        [documents.paragraph([
            _run_with_text("Knock knock"),
            documents.run([reference])
        ])],
        comments=[comment],
    )
    result = convert_document_element_to_html(document, id_prefix="doc-42-")
    expected_html = '<p>Knock knock</p>'
    assert_equal(expected_html, result.value)
def comments_are_ignored_by_default():
    reference = documents.comment_reference("4")
    comment = documents.comment(
        comment_id="4",
        body=[_paragraph_with_text("Who's there?")],
    )
    document = documents.document(
        [documents.paragraph([
            _run_with_text("Knock knock"),
            documents.run([reference])
        ])],
        comments=[comment],
    )
    result = convert_document_element_to_html(document, id_prefix="doc-42-")
    expected_html = '<p>Knock knock</p>'
    assert_equal(expected_html, result.value)
def footnotes_are_included_after_the_main_body():
    footnote_reference = documents.note_reference("footnote", "4")
    document = documents.document(
        [documents.paragraph([
            _run_with_text("Knock knock"),
            documents.run([footnote_reference])
        ])],
        notes=documents.notes([
            documents.note("footnote", "4", [_paragraph_with_text("Who's there?")])
        ])
    )
    result = convert_document_element_to_html(
        document,
        id_prefix="doc-42"
    )
    expected_html = ('<p>Knock knock<sup><a href="#doc-42-footnote-4" id="doc-42-footnote-ref-4">[1]</a></sup></p>' +
                '<ol><li id="doc-42-footnote-4"><p>Who\'s there? <a href="#doc-42-footnote-ref-4">↑</a></p></li></ol>')
    assert_equal(expected_html, result.value)
def footnotes_are_included_after_the_main_body():
    footnote_reference = documents.footnote_reference("4")
    document = documents.document(
        [
            documents.paragraph([
                _run_with_text("Knock knock"),
                documents.run([footnote_reference])
            ])
        ],
        footnotes=documents.Footnotes({
            "4":
            documents.Footnote("4", [_paragraph_with_text("Who's there?")])
        }))
    result = convert_document_element_to_html(document,
                                              generate_uniquifier=lambda: 42)
    expected_html = (
        '<p>Knock knock<sup><a href="#footnote-42-4" id="footnote-ref-42-4">[1]</a></sup></p>'
        +
        '<ol><li id="footnote-42-4"><p>Who\'s there? <a href="#footnote-ref-42-4">↑</a></p></li></ol>'
    )
    assert_equal(expected_html, result.value)
 def can_read_text_within_document(self):
     element = _document_element_with_text("Hello!")
     assert_equal(
         documents.document([documents.paragraph([documents.run([documents.Text("Hello!")])])]),
         _read_and_get_document_xml_element(element)
     )