question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to return the heading number

See original GitHub issue

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)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:11

github_iconTop GitHub Comments

4reactions
chdelfossecommented, May 28, 2019

a solution, call addHeaderNumbering just before saving the document

# --- from https://github.com/python-openxml/python-docx/issues/590,
# --- mods by CD
def iter_heading(paragraphs):
    for paragraph in paragraphs:
        isItHeading=re.match('Heading ([1-9])',paragraph.style.name)
        if isItHeading:
            yield int(isItHeading.groups()[0]),paragraph

def addHeaderNumbering(document):
    hNums=[0,0,0,0,0]
    for index,hx in iter_heading(document.paragraphs):
        # ---put zeroes below---
        for i in range(index+1,5):
            hNums[i]=0
        # ---increment this---
        hNums[index]+=1
        # ---prepare the string---
        hStr=""
        for i in range(1,index+1):
            hStr+="%d."%hNums[i]
        # ---add the numbering---
        hx.text=hStr+" "+hx.text
0reactions
disarticulatecommented, Oct 14, 2022

so i’ve looked into this several times. The issue appears to be that the underlying xml is very convoluted in how apps like Word generate actual heading numbers.

Here’s a primer: http://officeopenxml.com/WPnumbering.php

it seems implented in javascript’s docx here: https://docx.js.org/#/usage/numbering

Regardless, it’s pretty difficult to parse since it refers to document level metadata, which tracks numbering not as shown but as calculated through whatever is happening in the document.

While this would be a great feature, I can see already someone would need to spend quite a bit of energy getting it to work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Number your headings - Microsoft Support
Open your document that uses built-in heading styles, and select the first Heading 1. · On the Home tab, in the Paragraph group,...
Read more >
How to number headings in a Word 2016 document
Click inside the Number format control–to the left of the example character–and enter Heading, as shown in Figure D. Click OK twice. If...
Read more >
Retrieving the Column Header that Corresponds with a ...
COLUMN: Returns the column number of a reference. Syntax: =COLUMN(reference). Let us take an example: We have 3 column headers that contain numeric...
Read more >
How to extract heading numbers from a document using ...
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our ...
Read more >
Get column header based on specific row value with formula
Do you mean to search for multiple values and return matching headers in a single cell? If so, simply join the same formula...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found