Run2 docx recursive text replacement
Replaces placeholders in a Word document across all sections, including headers, footers, body paragraphs, and deeply nested tables. It uses paragraph-level replacement to ensure placeholders split across multiple runs are correctly identified.From its SKILL.md
npx -y skills add cxcscmu/SkillLearnBench --skill run2_docx_recursive_text_replacementAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
1.8 KB, 326 tokens by cl100k_base, as published. Nobody here has run it
To ensure all placeholders (e.g., {{PLACEHOLDER}}) are replaced regardless of their location or document structure, follow this approach using python-docx:
- Iterate all Sections: Loop through
doc.sectionsto accessheaderandfooter. - Recursive Table Processing: Create a function that iterates through a table's rows and cells. For every cell, it must check
cell.paragraphsand recursively call itself for any tables found incell.tables. - Paragraph-Level Replacement: Instead of iterating through
paragraph.runs, perform the replacement onparagraph.text. This avoids issues where Word splits a single placeholder into multiple XML "runs". - Complete Coverage: Apply the replacement logic to:
doc.paragraphsdoc.tables(using the recursive function)section.header.paragraphsandsection.header.tablessection.footer.paragraphsandsection.footer.tables
Example logic:
def replace_in_paragraph(paragraph, data):
for key, value in data.items():
placeholder = f"{{{{{key}}}}}"
if placeholder in paragraph.text:
paragraph.text = paragraph.text.replace(placeholder, str(value))
def process_table(table, data):
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
replace_in_paragraph(paragraph, data)
for nested_table in cell.tables:
process_table(nested_table, data)
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.