Dhanasekaran R
Dhanasekaran R

Reputation: 19

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. I am using docx module to work with documents. When I try to read a heading from docx file using paragraph.style.name, I am getting:

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.

Upvotes: 0

Views: 1914

Answers (1)

Edward Minnix
Edward Minnix

Reputation: 2947

This means that something which you are accessing an attribute on is None (not a real value).

You need to check paragraph.style if it is None, and not access .style.name.

if paragraph.style is not None and paragraph.style.name == 'Heading 1':
  print(paragraph.text)

Upvotes: 1

Related Questions