python - AttributeError: 'NoneType' object has no attribute 'name' 当我尝试获取 docx 文件的标题时发生错误
问题描述
我是 python 编程的新手。我正在使用docx
模块来处理文档。当我尝试使用从 docx 文件中读取标题时paragraph.style.name
,我得到:
AttributeError: 'NoneType' object has no attribute 'name'
我的脚本:
from docx import Document
document=Document('C:\\Users\\abc\\Desktop\\check\\Leave_Policy_converted.docx')
for paragraph in document.paragraphs:
if paragraph.style.name == 'Heading 1':
print (paragraph.text)
请澄清一下。先感谢您。
解决方案
这意味着您正在访问属性的东西是None
(不是真正的值)。
你需要检查paragraph.style
它是否是None
,而不是访问.style.name
。
if paragraph.style is not None and paragraph.style.name == 'Heading 1':
print(paragraph.text)
推荐阅读
- postgresql - 如何从不再存在的数据中清除 docker 卷?
- android - 如何在另一个布局中使用 ScrollView?
- xamarin - 如何将 android 活动导航到 xamarin 表单
- docker - Docker/Docker-Compose:无法为服务 tomcat 创建容器:stat /dev/log/:不是目录
- python-3.x - 使用 Google-cloud-texttospeech 时出现 Python 错误
- java - Hyperledger Fabric 调用 Java 链码(作为外部服务)超时
- typescript - 打字稿:使用排除时,类型 T 不可分配给类型“从不”
- excel - 遍历一列并根据单元格值将数据复制到另外两列
- amazon-web-services - CloudFormation 漂移分析 IAM 角色
- node.js - res.cookie 在本地主机上设置令牌,但不在服务器上