forked from mikemaccana/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-extracttext.py
executable file
·30 lines (26 loc) · 1.09 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
#!/usr/bin/env python2.6
'''
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.
'''
from docx import *
import sys
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:')
print(''' example-extracttext.py 'My Office 2007 document.docx' 'outputfile.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 our documnts test with two newlines under each paragraph
newfile.write('\n\n'.join(newparatextlist))
#print '\n\n'.join(newparatextlist)