Skip to main content
 首页 » 编程设计

python之导入错误 : cannot import name opendocx

2025年05月04日13zhengyun_ustc

我正在尝试使用以下代码从 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

一些文本 '.......' 来发布问题。

请您参考如下方法:

请阅读, 'opendocx()' 函数不再是最新版本的 python-docx 的一部分。从 v0.3.0 开始,python-docx 已经完全重写,API 不向后兼容。新的电话会是这样的:

document = Document(docx_file_path) 

有关新版本的文档可在此处获得: http://python-docx.readthedocs.org/

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

pip install docx 

两个版本之间的包名称发生了变化,因此人们仍然可以访问旧版本,如果他们想要的话。您应该在安装 docx 之前卸载 python-docx,反之亦然,以避免混淆正在导入的内容。

如果您需要更多,请告诉我。

引用:https://groups.google.com/forum/#!msg/python-docx/otp6hq4kJ5c/tfQB88Mfx2gJ