| name | docx-edit |
| description | DOCX: Edit an existing Word document — append content, replace text, or add tables. |
| metadata | {"version":"0.22.0","maxent":{"requires":{"bins":[{"name":"python","install":{"windows":{"method":"winget","id":"Python.Python.3"},"linux":{"method":"apt","id":"python3"},"darwin":{"method":"brew","id":"python"}}}]}}} |
docx-edit
PREREQUISITE: Ensure python-docx is installed: pip install python-docx
Modify an existing .docx Word document — append content, search and replace text, remove paragraphs, or add tables and headings.
Usage
python - <<'EOF'
from docx import Document
doc = Document("existing.docx")
doc.save("existing.docx")
EOF
Always read the file with docx-read first to understand its current structure before editing.
Recipes
Append a paragraph
python - <<'EOF'
from docx import Document
doc = Document("report.docx")
doc.add_paragraph("Appended paragraph at the end.")
doc.save("report.docx")
EOF
Append a heading and paragraph
python - <<'EOF'
from docx import Document
doc = Document("report.docx")
doc.add_heading("New Section", level=1)
doc.add_paragraph("Content for the new section.")
doc.save("report.docx")
EOF
Search and replace text across all paragraphs
python - <<'EOF'
from docx import Document
doc = Document("report.docx")
old, new = "Q3 2024", "Q4 2024"
for para in doc.paragraphs:
if old in para.text:
for run in para.runs:
run.text = run.text.replace(old, new)
doc.save("report.docx")
EOF
Search and replace text including inside tables
python - <<'EOF'
from docx import Document
def replace_in_paragraphs(paragraphs, old, new):
for para in paragraphs:
for run in para.runs:
if old in run.text:
run.text = run.text.replace(old, new)
doc = Document("report.docx")
old, new = "Draft", "Final"
replace_in_paragraphs(doc.paragraphs, old, new)
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
replace_in_paragraphs(cell.paragraphs, old, new)
doc.save("report.docx")
EOF
Remove paragraphs matching a condition
python - <<'EOF'
from docx import Document
from docx.oxml.ns import qn
doc = Document("report.docx")
to_remove = []
for para in doc.paragraphs:
if "DELETE ME" in para.text:
to_remove.append(para._element)
for el in to_remove:
el.getparent().remove(el)
doc.save("report.docx")
EOF
Append a table
python - <<'EOF'
from docx import Document
doc = Document("report.docx")
table = doc.add_table(rows=1, cols=2)
table.style = "Table Grid"
hdr = table.rows[0].cells
hdr[0].text = "Key"
hdr[1].text = "Value"
rows_data = [("Status", "Complete"), ("Owner", "Alice")]
for key, val in rows_data:
row = table.add_row().cells
row[0].text = key
row[1].text = val
doc.save("report.docx")
EOF
Save as a new file (non-destructive)
python - <<'EOF'
from docx import Document
doc = Document("report.docx")
doc.add_paragraph("Extra note added.")
doc.save("report_v2.docx")
EOF
Tips
- Always inspect first with
docx-read before editing so you know paragraph indices, style names, and table layout.
- Search & replace via runs:
para.text is read-only; mutations must go through run.text.
- Paragraph removal: Access
para._element and call .getparent().remove(el) — there is no built-in remove_paragraph API.
- Save path:
doc.save(same_path) overwrites in place. Use a different path to preserve the original.
- Tables from
doc.tables only returns top-level tables; nested tables must be accessed via cell.tables.
[!CAUTION]
This is a write command — confirm with the user before executing. Overwriting doc.save(original_path) is irreversible unless a backup exists.
See Also