-1

I am trying to a loop from the first to last line of a word document. I am using python-docx package. Documents deals with paragraphs & tables as well. What I want to do is: Write a for loop from first to last of the document and do something for paragraphs & tables

How do I iterate through each line in python?

Nupur B
  • 1
  • 3

2 Answers2

0

Pass the filepath to the following function:

import docx

def getText(filepath):
    doc = docx.Document(filepath)
    fullText = []
    for para in doc.paragraphs:
        fullText.append(para.text)
    return '\n'.join(fullText)

it will return you a list of lines. (Lines here might be different than the line you see in the document)

then you can iterate using:

for paragraph in getText(filepath).split('\n'):
    # do what you will with the line
kernel
  • 49
  • 6
0

Something like this would help:

import docx
doc = docx.Document('your file')
for i in doc.paragraphs:
     do something
Neal Titus Thomas
  • 483
  • 1
  • 6
  • 16