summaryrefslogtreecommitdiff
path: root/mammoth/__init__.py
blob: 889cdd5dee883d6bb737cb79591f9690e17d3a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from . import docx, conversion, options, documents, images

__all__ = ["convert_to_html", "extract_raw_text", "images"]


def convert_to_html(fileobj, transform_document=None, **kwargs):
    if transform_document is None:
        transform_document = lambda x: x
    return docx.read(fileobj).map(transform_document).bind(lambda document: 
        conversion.convert_document_element_to_html(
            document,
            **options.read_options(kwargs)
        )
    )


def extract_raw_text(fileobj):
    return docx.read(fileobj).map(_extract_raw_text_from_element)


def _extract_raw_text_from_element(element):
    if isinstance(element, documents.Text):
        return element.value
    else:
        text = "".join(map(_extract_raw_text_from_element, element.children))
        if isinstance(element, documents.Paragraph):
            return text + "\n\n"
        else:
            return text