Skip to content
Skills Library
Documents & office · Automation

python-docx-nested-table-recursion

/python-docx-nested-table-recursion

Detailed guidance on recursively processing nested tables in python-docx documents. Use this when a Word template contains tables inside table cells and you need to reach all paragraphs.

Licence
MIT
Last repository change
02.10.2026

Assessment and security

90/ 100Class: good
Format20 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content30 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 02.10.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Detailed guidance on recursively processing nested tables in python-docx documents. Use this when a Word template contains tables inside table cells and you need to reach all paragraphs.

Original author description

Detailed guidance on recursively processing nested tables in python-docx documents. Use this when a Word template contains tables inside table cells and you need to reach all paragraphs.

How it works

  1. The Problem
  2. Recursive Function
  3. Handling Duplicate Cells from Merged Cells
  4. Full Document Traversal
  5. Testing Nested Tables
Original SKILL.md headings · 94 lines, 349 words
SKILL.md preview
---
name: python-docx-nested-table-recursion
description: Detailed guidance on recursively processing nested tables in python-docx documents. Use this when a Word template contains tables inside table cells and you need to reach all paragraphs.
---

## The Problem

`python-docx`'s `Document.tables` only returns **top-level** tables. If a table cell contains another table (nested table), you must explicitly access `cell.tables` to reach it. This can nest arbitrarily deep.

## Recursive Function

```python
def iter_all_paragraphs_in_table(table):
    """Recursively yield all paragraphs in a table, including nested tables."""
    for row in table.rows:
        for cell in row.cells:
            for paragraph in cell.paragraphs:
                yield paragraph
            # Recurse into nested tables
            for nested_table in cell.tables:
                yield from iter_all_paragraphs_in_table(nested_table)
```

## Handling Duplicate Cells from Merged Cells

When cells are merged in a table, iterating `row.cells` can return the same `Cell` object multiple times. To avoid processing the same paragraph twice:

```python
def iter_all_paragraphs_in_table_deduped(table):
    seen = set()
    for row in table.rows:
        for cell in row.cells:
            cell_id = id(cell._element)
            if cell_id in seen:
                continue
            seen.add(cell_id)
            for paragraph in cell.paragraphs:
                yield paragraph
            for nested_table in cell.tables:
                yield from iter_all_paragraphs_in_table_deduped(nested_table)
```

## Full Document Traversal

```python
def iter_all_doc_paragraphs(doc):
    """Yield every paragraph in the entire document."""
    seen = set()
    
    def _yield_para(p):
        pid = id(p._element)
        if pid not in seen:
            seen.add(pid)
            yield p
    
    # Body paragraphs
    for p in doc.paragraphs:
        yield from _yield_para(p)
    
    # Body tables
    for table in doc.tables:
        for p in iter_all_paragraphs_in_table_deduped(table):
            yield from _yield_para(p)
    
    # All sections: headers and footers
    for section in doc.sections:
        for hf in [section.header, section.footer,
                    section.first_page_header, section.first_page_footer,
                    section.even_page_header, section.even_page_footer]:
            if hf is None or not hf.is_linked_to_previous:
                pass  # process anyway to be safe
            if hf is not None:
                for p in hf.paragraphs:
                    yield from _yield_para(p)
                for table in hf.tables:
                    for p in iter_all_paragraphs_in_table_deduped(table):
                        yield from _yield_para(p)
```

**Note on `is_linked_to_previous`**: Even if a header/footer is linked to previous, it's safer to process it. The `is_linked_to_previous` property means it shares content with the previous section's header/footer, but iterating it won't cause harm.

## Testing Nested Tables

To verify your code reaches nested table content:

```python
doc = Document('template.docx')
all_texts = []
for p in iter_all_doc_paragraphs(doc):
    text = ''.join(run.text for run in p.runs)
    if '{{' in text:
        all_texts.append(text)
print(f"Found {len(all_texts)} paragraphs with placeholders")
for t in all_texts:
    print(f"  - {t}")
```

This helps identify all placeholders that need replacement, including those buried in nested tables.

Versions

The database has no other skills named python-docx-nested-table-recursion.

Database snapshot: 13.11.2026 · package version 76e49132a448 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 95Docx
    Word document processing. USE WHEN docx, Word document. SkillSearch('docx') for docs.
  • 100docx-generator
    Create, read, and edit Word (.docx) files - styles, tables, images, headers, and templates - using python-docx. Use whenever a deliverable must be a Word document.
  • 100doc-skill-docx
    Creates and edits Word documents with python-docx: styles, tables, headers, footers, track-changes-safe edits. Use when the user asks for docx document skill work, or mentions doc, skill, docx.
  • 100python-docx-templating
    How to process Word documents (.docx) with python-docx. Use this skill whenever the user mentions Word templates, docx templating, replacing placeholders, or processing Word files, even if they don't explicitly ask for it.
  • 100docx
    Create and edit Word documents (.docx): headings, styled paragraphs, tables, images, headers/footers, and page formatting. Use whenever the user asks to write, format, or modify a Word/.docx document.
  • 100zepi-docx
    Read and edit Word documents with python-docx on the bundled runtime — styles, numbering, tables, headers and footers, page breaks, images, find-and-replace across runs, and merging documents. Use for any task whose input or output is a .docx file, including extracting text or…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.