Skip to content

How to return the heading number #590

@tfitzhardinge

Description

@tfitzhardinge

Hi

I have managed to return the heading text in a given filename (thank you to the great experts at Stack Overflow) however I cannot return the heading number. Refer to code below. Is there a way to print/return the heading number (list numbering value not heading level).

Thanks

import docx

doc=docx.Document('filename.docx')

def iter_heading(paragraphs):
    for paragraph in paragraphs:
        if paragraph.style.name('Heading 1'):
            yield paragraph

for heading in iter_heading(doc.paragraphs):
    print(heading.text)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions