WolfDocx documentation - Community
Read paragraphs and tables from an existing DOCX in Python
WolfDocx Community reads an existing DOCX through Document and exposes a typed node tree. Select paragraph or table descendants with NodeType, then call get_text for their text. This is structural inspection of Word content, not page rendering or a plain-text reconstruction of every document feature.
Read without saving
Install the public Community package with pip install wolfdocx in a Python 3.11 or newer environment. This example targets Community 0.1.0 and uses only its public API. Put an existing document at input.docx, then run the Python script below. No output file is created because it makes no edit and never calls save.
from wolfdocx import Document, NodeType
with Document("input.docx") as doc:
paragraphs = doc.node.get_child_nodes(NodeType.PARAGRAPH, deep=True)
for index, paragraph in enumerate(paragraphs, start=1):
print("paragraph", index, repr(paragraph.get_text()))
tables = doc.node.get_child_nodes(NodeType.TABLE, deep=True)
for index, table in enumerate(tables, start=1):
print("table", index, repr(table.get_text()))The first traversal returns paragraphs below the document node, including those nested in tables. The second returns table nodes separately. If you combine both lists into one text export, table content can appear twice. Decide whether you want a paragraph inventory or a table inventory before aggregating their text.
get_text returns the node's structural text with delimiters. Paragraph text can end in a carriage return; the example prints repr so that boundary is visible. Do not count these delimiters as literal prose or infer a physical line from each paragraph. Word wraps text during layout, not by inserting one paragraph per displayed line.
Traversal parameters
| Parameter | Meaning in this example |
|---|---|
NodeType.PARAGRAPH | Filter the descendants to paragraph nodes. |
NodeType.TABLE | Filter the descendants to table nodes. |
deep=True | Traverse recursively rather than inspect only immediate children. |
A recursive filter answers which matching nodes occur below a root. It does not supply a joined sequence of paragraph and table records for a separate interchange format. Keep the original node hierarchy if your application needs to distinguish body text, table text, or nested content.
The context manager owns the document session while you inspect it. Keep node access inside that context rather than storing live nodes for later use after the document is closed. If you need a durable record, collect ordinary strings or your own structured data while the session is open.
Read before selecting an edit
Inspection is the first step in a targeted update. Read the reviewed source, identify the paragraph or cell you intend to change, and then use an editing API with the appropriate guards. A human-readable table number printed by this script is an inventory aid; it is not a substitute for checking the target's current text.
For example, a report may contain the same word in the body and in a table. A broad regular-expression replacement is a different operation from setting one cell. Use the task's actual boundary rather than assuming a text search identifies exactly one business value. The editing guide shows a cell request with explicit coordinates and expected old text.
Reading a DOCX also differs from extracting visible pages. Headers, footers, field results, and drawings can have their own structure and lifecycle. This short example does not claim to flatten every such feature into one faithful text transcript. Use an explicit story-specific workflow when the requested operation depends on those parts.
What it does not do
- It does not render pages, calculate visual coordinates, or return page numbers.
- It does not perform OCR on pictures or reconstruct text from image pixels.
- It does not promise a lossless plain-text export of all Word features.
- It does not update field caches just by reading their containing document.
Related documentation
Start with installation and quickstart, continue to guarded editing, or read the task guide for updating an existing Word table.