forked from mikemaccana/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-extracttext.py
executable file
·37 lines (29 loc) · 1.04 KB
/
example-extracttext.py
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
30
31
32
33
34
35
36
37
#!/usr/bin/env python
"""
This file opens a docx (Office 2007) file and dumps the text.
If you need to extract text from documents, use this file as a basis for your
work.
Part of Python's docx module - http://github.com/mikemaccana/python-docx
See LICENSE for licensing information.
"""
import sys
from docx import opendocx, getdocumenttext
if __name__ == '__main__':
try:
document = opendocx(sys.argv[1])
newfile = open(sys.argv[2], 'w')
except:
print(
"Please supply an input and output file. For example:\n"
" example-extracttext.py 'My Office 2007 document.docx' 'outp"
"utfile.txt'"
)
exit()
# Fetch all the text out of the document we just created
paratextlist = getdocumenttext(document)
# Make explicit unicode version
newparatextlist = []
for paratext in paratextlist:
newparatextlist.append(paratext.encode("utf-8"))
# Print out text of document with two newlines under each paragraph
newfile.write('\n\n'.join(newparatextlist))