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

2024-05-19 07:05:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python脚本来转换文件.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/

有人能帮帮我吗?在


Tags: 文件pathfromimport脚本docstdoutfilename
1条回答
网友
1楼 · 发布于 2024-05-19 07:05:43

我现在也在讨论这个问题,据我所知,python没有直接的API。 但您始终可以从命令行使用此命令。在

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

从python运行这个命令。在

^{pr2}$

相关问题 更多 >

    热门问题