如何在Windows上安装antiword并在Python中使用它

3

我正在使用Python脚本将一个file.doc文件转换成file.txt文件。我的代码如下:

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

#https://dev59.com/fW025IYBdhLWcg3w6aUX
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/ 有人能帮忙吗?

如果您阅读了您链接的页面,您会找到安装说明 - Matthias
1个回答

2

我现在也在这个问题上,据我所知,Python没有直接的API来解决这个问题。 但是你可以通过命令行来解决。

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

从Python运行此命令。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接