我是python编程的新手。我正在使用docx
模块来处理文档。当我尝试使用paragraph.style.name
从docx文件中读取标题时,我得到:
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)