Docx库不识别Document方法

6 投票
1 回答
3704 浏览
提问于 2025-04-18 16:03

我正在尝试使用docx这个库,但出现了这个错误:

AttributeError: 'module' object has no attribute 'Document'

这是我的代码:

import docx
document = docx.Document('file.docx')

发生了什么事?我在其他电脑上已经用过这个文档了。我是通过pip安装的docx库:

pip install docx

1 个回答

12

你把包 docx 和包 python-docx 搞混了。

pip install python-docx


>>> import docx
>>> docx.Document
<class 'docx.api.Document'>

不过你说得对,这里命名确实有点问题。

撰写回答