gpt4 book ai didi

python - 如何在windows上安装antiword并在python中使用

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:30 51 4
gpt4 key购买 nike

我正在使用 python 脚本将 file.doc 转换为文件。 TXT。我的代码是:

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')

关于python - 如何在windows上安装antiword并在python中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40205704/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com