forked from puerdon/corpus_processor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b7e58d
commit 820f726
Showing
3 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
try: | ||
from xml.etree.cElementTree import XML | ||
except ImportError: | ||
from xml.etree.ElementTree import XML | ||
import zipfile | ||
|
||
|
||
""" | ||
Module that extract text from MS XML Word document (.docx). | ||
(Inspired by python-docx <https://github.com/mikemaccana/python-docx>) | ||
""" | ||
|
||
WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}' | ||
PARA = WORD_NAMESPACE + 'p' | ||
TEXT = WORD_NAMESPACE + 't' | ||
|
||
|
||
def get_docx_text(path): | ||
""" | ||
Take the path of a docx file as argument, return the text in unicode. | ||
""" | ||
document = zipfile.ZipFile(path) | ||
xml_content = document.read('word/document.xml') | ||
document.close() | ||
tree = XML(xml_content) | ||
|
||
paragraphs = [] | ||
for paragraph in tree.getiterator(PARA): | ||
texts = [node.text | ||
for node in paragraph.getiterator(TEXT) | ||
if node.text] | ||
if texts: | ||
paragraphs.append(' '.join(texts)) | ||
|
||
return '\n\n'.join(paragraphs) |