OOXML Template Filler
Fills Word (.docx) templates that use {{PLACEHOLDER}} syntax with values from a JSON data file.
Key Challenges Handled
- Split placeholders: Word may split
{{SOME_VAR}}across multiple<w:r>runs. The skill merges adjacent runs to reconstruct complete placeholders before replacement. - Conditional sections:
{{IF_RELOCATION}}...{{END_IF_RELOCATION}}blocks are kept (with markers removed) when the condition is met, or entirely removed when not. - All document parts: Processes document.xml, headers, and footers.
- Validation: Confirms no unresolved placeholders or conditional markers remain.
Usage
pythonimport sys sys.path.insert(0, '/app/environment/skills/evo-docx-template-filler/scripts') from utils import fill_template errors = fill_template( template_path='/root/offer_letter_template.docx', json_path='/root/employee_data.json', output_path='/root/offer_letter_filled.docx' ) if errors: print("Validation errors:") for e in errors: print(f" - {e}") else: print("Template filled successfully with no errors.")
Functions
load_employee_data(json_path)- Load JSON dataget_xml_parts_to_process(zip_path)- Find XML parts with potential placeholdersmerge_split_placeholders(xml_content)- Merge runs with split placeholdersreplace_placeholders(xml_bytes, data)- Replace{{KEY}}with valueshandle_conditional_sections(xml_bytes, data)- Process IF/END_IF blocksprocess_template(template_path, output_path, data)- End-to-end processingvalidate_output(output_path)- Check for unresolved placeholdersfill_template(template_path, json_path, output_path)- Main entry point


