Is there a reasonable way to extract plain text from a Word file that doesn’t depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform – that’s non-negotiable in this case.)
Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.
A Python solution would be ideal, but doesn’t appear to be available.
Advertisement
Answer
I use catdoc or antiword for this, whatever gives the result that is the easiest to parse. I have embedded this in python functions, so it is easy to use from the parsing system (which is written in python).
JavaScript
x
16
16
1
import os
2
3
def doc_to_text_catdoc(filename):
4
(fi, fo, fe) = os.popen3('catdoc -w "%s"' % filename)
5
fi.close()
6
retval = fo.read()
7
erroroutput = fe.read()
8
fo.close()
9
fe.close()
10
if not erroroutput:
11
return retval
12
else:
13
raise OSError("Executing the command caused an error: %s" % erroroutput)
14
15
# similar doc_to_text_antiword()
16
The -w switch to catdoc turns off line wrapping, BTW.