2016-10-23 123 views
1

file.docをファイルに変換するためにPythonスクリプトを使用しています。 TXT。私のコードは次のとおりです。windowsにantiwordをインストールして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) 

i「はantiword」をインストールする必要がありますが、問題は、私はそれを行う方法がわからないということです上記のスクリプトを使用するために。 ここに「アンチワード」をダウンロードするリンクがあります:http://www-stud.rbi.informatik.uni-frankfurt.de/~markus/antiword/

誰かが私を助けることができますか?

+0

あなたがにリンクされたページを読んでいる場合は、[インストール手順](http://www-stud.rbi.informatik.uni-frankfurt.de/~markus/antiword/00READMEを見つけることができます。勝つ)。 – Matthias

答えて

1

私はこれも今、私が理解しているように、このための直接的なAPIはありません。 しかし、これはコマンドラインからいつでも使用できます。

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

そしてこのコマンドをpythonから実行します。

os.system('antiword foo.doc > foo.txt') 
関連する問題