Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri May 16, 2025 7:20 pm

I’m trying to extract all paragraphs of a UN regulation from a Word document using Spire.Doc in Python.

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)

286004316
 
Posts: 1
Joined: Fri May 16, 2025 6:48 pm

Mon May 19, 2025 6:21 am

Hello,

Thanks for your inquiry.
May I ask which version of Spire.Doc for Python you are currently using? Please note that our Free Version imposes a limitation of 500 paragraphs and 25 tables when reading from or writing to Word files. If you are indeed using the Free Version, I would recommend considering our Commercial Version (Version:13.4.6). However, please be aware that even the Commercial Version has paragraph limitations if no license has been applied.
To help you fully experience all the features of the Commercial Version and evaluate our product more comprehensively, we can offer you a one-month temporary trial license. If you would like to request this temporary license, please feel free to let me know.

Sincerely,
Tommy
E-iceblue support team
User avatar

Tommy.Tang
 
Posts: 85
Joined: Mon Apr 21, 2025 7:05 am

Return to Spire.Doc