I am looking to extract text under headings in a docx file. The text structure kinda looks like this:
1. DESCRIPTION
Some text here
2. TERMS AND SERVICES
2.1 Some text here
2.2 Some text here
3. PAYMENTS AND FEES
Some text here
What I am looking for is something like this:
['1. DESCRIPTION','Some text here']
['2. TERMS AND SERVICES','2.1 Some text here 2.2 Some text here']
['3. PAYMENTS AND FEES', 'Some text here']
I have tried using python-docx library:
from docx import Document
document = Document('Test.docx')
def iter_headings(paragraphs):
for paragraph in paragraphs:
if paragraph.style.name.startswith('Normal'):
yield paragraph
for heading in iter_headings(document.paragraphs):
print (heading.text)
The styles that I have differ between Normal, Body Text and Heading #. Like sometimes the headings are Normal and the text for that section is in Body Text style. Can someone please guide me in the right direction. Will really appreciate it.