[英]How to install antiword on windows and use it in python
我正在使用 python 腳本將 file.doc 轉換為文件。 文本。 我的代碼是:
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)
為了使用上面的腳本,我需要安裝“antiword”,但問題是我不知道該怎么做。 這是下載“antiword”的鏈接: http ://www-stud.rbi.informatik.uni-frankfurt.de/~markus/antiword/
有人可以幫助我嗎?
我現在也在做這個,據我所知,python 沒有直接的 API。 但是您始終可以從命令行使用它。
antiword -f file.doc > file.txt
antiword -p letter file.doc > file.pdf
並從 python 運行此命令。
os.system('antiword foo.doc > foo.txt')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.