How to return the heading number
See original GitHub issueHi
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:
- Created 5 years ago
- Comments:11
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

a solution, call addHeaderNumbering just before saving the document
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.