From 32e20e3ff46807bff9eaee2010253c708af5649a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 6 Jul 2014 17:55:50 +0100 Subject: Add extract_raw_text function --- mammoth/__init__.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'mammoth') diff --git a/mammoth/__init__.py b/mammoth/__init__.py index 59e349f..05e2167 100644 --- a/mammoth/__init__.py +++ b/mammoth/__init__.py @@ -1,5 +1,5 @@ from .results import Result -from . import docx, conversion, options +from . import docx, conversion, options, documents def convert_to_html(fileobj, transform_document=None, **kwargs): @@ -11,3 +11,18 @@ def convert_to_html(fileobj, transform_document=None, **kwargs): **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 -- cgit v1.2.3