2016-10-23 3 views
1

J'utilise un script python pour convertir un fichier.doc en un fichier. SMS. Mon code est:Comment installer antiword sur Windows et l'utiliser en python

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) 

Pour utiliser le script ci-dessus que je dois installer « antiword », mais le problème est que je ne sais pas comment le faire. Voici le lien où télécharger 'antiword': http://www-stud.rbi.informatik.uni-frankfurt.de/~markus/antiword/

Quelqu'un peut-il m'aider?

+0

Si vous lisez la page vous avez accédé à, vous trouverez les instructions d'installation [] (http://www-stud.rbi.informatik.uni-frankfurt.de/~markus/antiword/00README. GAGNER). – Matthias

Répondre

1

Je suis également sur ce maintenant et comme je l'ai compris python n'a pas d'API directe pour cela. Mais vous pouvez toujours l'utiliser depuis la ligne de commande.

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

Exécutez cette commande à partir de python.

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