aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-07-06 17:55:50 +0100
committerMichael Williamson <mike@zwobble.org>2014-07-06 17:55:50 +0100
commit32e20e3ff46807bff9eaee2010253c708af5649a (patch)
treef0ec9d1a051a03c77d86b2c9d7c826fdeae1b415
parent42161df2673dd9d649e31dd0cc265738300af771 (diff)
Add extract_raw_text function
-rw-r--r--mammoth/__init__.py17
-rw-r--r--tests/mammoth_tests.py9
2 files changed, 25 insertions, 1 deletions
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
diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py
index 0b3bf0f..9721d15 100644
--- a/tests/mammoth_tests.py
+++ b/tests/mammoth_tests.py
@@ -54,3 +54,12 @@ def transform_document_is_applied_to_document_before_conversion():
result = mammoth.convert_to_html(fileobj=fileobj, transform_document=transform_document)
assert_equal("<h1>Walking on imported air</h1>", result.value)
assert_equal([], result.messages)
+
+
+@istest
+def can_extract_raw_text():
+ with open(test_path("simple-list.docx"), "rb") as fileobj:
+ result = mammoth.extract_raw_text(fileobj=fileobj)
+ assert_equal([], result.messages)
+ assert_equal("Apple\n\nBanana\n\n", result.value)
+