CosimoCD
CosimoCD

Reputation: 3760

How to install antiword on windows and use it in python

I'm using a python script in order to convert a file.doc into a file. txt. My code is:

from subprocess import Popen, PIPE
from docx import opendocx, getdocumenttext

#http://stackoverflow.com/questions/5725278/python-help-using-pdfminer-as-a-library
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO
import os

def document_to_text(filename, file_path):
    if filename[-4:] == ".doc":
       cmd = ['antiword', file_path]
       p = Popen(cmd, stdout=PIPE)
       stdout, stderr = p.communicate()
       return stdout.decode('ascii', 'ignore')
   elif filename[-5:] == ".docx":
       document = opendocx(file_path)
       paratextlist = getdocumenttext(document)
       newparatextlist = []
       for paratext in paratextlist:
         newparatextlist.append(paratext.encode("utf-8"))
       return '\n\n'.join(newparatextlist)

In order to use the script above i need to install 'antiword' but the problem is that i don't know how to do it. Here is the link where do download 'antiword':http://www-stud.rbi.informatik.uni-frankfurt.de/~markus/antiword/

Can somebody help me?

Upvotes: 3

Views: 6840

Answers (1)

Raymond Reddington
Raymond Reddington

Reputation: 1837

I'm also on this now and as I understood python doesn't have direct API for this. But you can always use this from command line.

antiword -f file.doc > file.txt
antiword -p letter file.doc > file.pdf

And run this command from python.

os.system('antiword foo.doc > foo.txt')

Upvotes: 2

Related Questions