简体   繁体   English

AttributeError: 'NoneType' 对象没有属性 'name' 错误在我尝试获取 docx 文件的标题时发生

[英]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.

相关问题 “NoneType”对象没有“插入”属性。 当我尝试在数组中插入弹出值时发生此错误 - 'NoneType' object has no attribute 'insert'. This error occurs when I try to insert popped value in array AttributeError: 'NoneType' object 在写入文件时没有属性 'get' - AttributeError: 'NoneType' object has no attribute 'get' when writing into a file 我收到此错误 --> AttributeError: 'NoneType' object has no attribute 'predict' - I get this error --> AttributeError: 'NoneType' object has no attribute 'predict' AttributeError:当我保存在age中时,“ NoneType”对象没有属性“ get” - AttributeError: 'NoneType' object has no attribute 'get' when I save in amage 尝试将 append 加入列表时,出现错误“AttributeError: 'NoneType' object has no attribute 'append'” - When trying to append to a list, I get the error "AttributeError: 'NoneType' object has no attribute 'append'" AttributeError: 'NoneType' object 没有属性 'send',当我尝试使用机器人向特定 discord 通道发送消息时 - AttributeError: 'NoneType' object has no attribute 'send' ,when I try to send a message with a bot to a specific discord channel Scrapyd错误-AttributeError:“ NoneType”对象没有属性“ module_name” - Scrapyd error - AttributeError: 'NoneType' object has no attribute 'module_name' AttributeError: 'NoneType' object 没有带有 streamlit 的属性 'name' - AttributeError: 'NoneType' object has no attribute 'name' with streamlit AttributeError:'NoneType'对象没有属性'name' - AttributeError: 'NoneType' object has no attribute 'name' Flask AttributeError: 'NoneType' object 没有属性 'name' - Flask AttributeError: 'NoneType' object has no attribute 'name'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM