导入错误: 无法导入名称 opendocx

2024-05-17 04:04:24 发布

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

我正在尝试使用以下代码从docx生成一个txt文件:

from subprocess import Popen, PIPE
from docx import opendocx, getdocumenttext
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

def convert_pdf_to_txt(path):
    ...

def document_to_text(filename, file_path):
    ...
    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)
    elif filename[-4:] == ".odt":
        ...
    elif filename[-4:] == ".pdf":
        ...

document_to_text('1.docx','D:\Nucho\Python\AntiPlagiat\1.docx')

但是,我只看到:ImportError: cannot import name opendocx

一些文本“……来发布问题。在


Tags: topathfromimporttxtpdfdeffilename
1条回答
网友
1楼 · 发布于 2024-05-17 04:04:24

请阅读, “opendocx()”函数不再是最新版本的python docx的一部分。从v0.3.0开始,python-docx已经被完全重写,而且API并不向后兼容。新电话应该是:

document = Document(docx_file_path)

有关新版本的文档可从以下位置获得: http://python-docx.readthedocs.org/

如果您想要以前的API,应该安装docx,而不是python-docx,例如:

^{pr2}$

包的名称在两个版本之间发生了变化,因此人们仍然可以访问遗留版本(如果他们需要的话)。您应该在安装docx之前卸载python docx,反之亦然,以避免混淆导入的对象。在

如果你还需要,请告诉我。在

参考号:https://groups.google.com/forum/#!msg/python-docx/otp6hq4kJ5c/tfQB88Mfx2gJ

相关问题 更多 >