簡體   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

我是 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM