When I use my get_all_paragraphs function , I only get 564 paragraphs, but the actual document should contain far more.
When I print text by iterating through all Section.Body.ChildObjects, I only see about 1/4 of the document’s actual content.
- Code: Select all
def get_all_paragraphs(doc: Document):
q = queue.Queue()
q.put(doc)
paragraphs = []
while not q.empty():
current = q.get()
if hasattr(current, "ChildObjects") and current.ChildObjects:
for i in range(current.ChildObjects.Count):
child = current.ChildObjects.get_Item(i)
if isinstance(child, Paragraph):
paragraphs.append(child)
elif isinstance(child, ICompositeObject):
q.put(child)
return paragraphs
doc = Document()
doc.LoadFromFile(input_file)
paragraphs = get_all_paragraphs(doc)
print(len(paragraphs)) # The result is much less than expected
# Or, by directly looping through Sections:
for s in range(doc.Sections.Count):
section = doc.Sections[s]
body = section.Body
for i in range(body.ChildObjects.Count):
obj = body.ChildObjects[i]
if isinstance(obj, Paragraph):
text = obj.Text.strip()
print(text)