Page number python-docx

Viewed 15583

I am trying to create a program in python that can find a specific word in a .docx file and return page number that it occurred on. So far, in looking through the python-docx documentation I have been unable to find how do access the page number or even the footer where the number would be located. Is there a way to do this using python-docx or even just python? Or if not, what would be the best way to do this?

2 Answers

Using Python-docx: identify a page break in paragraph

from docx import Document
fn='1.doc'
document = Document(fn)
pn=1    
import re
for p in document.paragraphs:
    r=re.match('Chapter \d+',p.text)
    if r:
        print(r.group(),pn)
    for run in p.runs:
        if 'w:br' in run._element.xml and 'type="page"' in run._element.xml:
            pn+=1
            print('!!','='*50,pn)
Related