user3818875
user3818875

Reputation:

reading heading from docx file using python

i want to read heading from a docx file but Document module does not have property of read heading how i can do that

from docx import Document
def docheading():
    document = Document('C://Users//yousafzai//Desktop//Database//riya//riya//AbdulMateen.docx')
    headings=document.heading

Upvotes: 1

Views: 2451

Answers (3)

bia
bia

Reputation: 131

This is how you can extract the title:


for paragraph in document.paragraphs:
    if paragraph.style.name=='Title':
        doc_title = paragraph.text

Upvotes: 1

Nisarg Mankad
Nisarg Mankad

Reputation: 23

I kinda did the same thing. I did something like this the attribute you are looking for is Style.name

for paragraph in paragraphs:
    if paragraph.style.name=='Heading 1':
        print (paragraph.text)

Upvotes: 2

AutomaticStatic
AutomaticStatic

Reputation: 1749

Try a different library, for example paradocx, which can read paradata from the Office XML spec using something like style='Heading 1'

Upvotes: 0

Related Questions