[英]AttributeError: 'NoneType' object has no attribute 'name' error occurs when I try to get a heading of a docx file
I am new to python programming.我是 python 编程的新手。 I am using
docx
module to work with documents.我正在使用
docx
模块来处理文档。 When I try to read a heading from docx file using paragraph.style.name
, I am getting:当我尝试使用读取DOCX从文件标题
paragraph.style.name
,我得到:
AttributeError: 'NoneType' object has no attribute 'name'
My Script:我的脚本:
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)
Please clarify me.请澄清我。 Thank you in advance.
先感谢您。
This means that something which you are accessing an attribute on is None
(not a real value).这意味着您正在访问属性的内容是
None
(不是真正的值)。
You need to check paragraph.style
if it is None
, and not access .style.name
.你需要检查
paragraph.style
如果是None
,而不是访问.style.name
。
if paragraph.style is not None and paragraph.style.name == 'Heading 1':
print(paragraph.text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.