A comprehensive validation tool for python-docx-template (docxtpl) Word templates
Project description
Docxtpl Template Checker
A comprehensive validation tool for python-docx-template (docxtpl) Word templates. This tool helps you find and fix syntax errors in your Jinja2 templates embedded in Word documents, ensuring your templates work correctly before processing.
โจ Features
- ๐ Comprehensive Syntax Validation: Detects Jinja2 and docxtpl-specific syntax errors
- ๐ Detailed Error Reports: Generates markdown reports with precise error locations and context
- ๐ Multi-language Support: Full localization in English and Chinese (ไธญๆ)
- ๐ Context-aware Error Location: Shows exact line numbers and surrounding context
- ๐ท๏ธ Docxtpl-specific Validation: Understands
{%p %},{%tr %},{%tc %},{%r %}prefix tags - ๐ฏ Smart Error Detection: Identifies unmatched tags, bracket/quote issues, and common mistakes
- ๐ Multiple Output Formats: Terminal summary and detailed markdown reports
- ๐ ๏ธ Command-line Interface: Easy integration into CI/CD pipelines
๐ฆ Installation
Prerequisites
- Python 3.6 or higher
jinja2library
Install Dependencies
pip install jinja2
Download
git clone https://github.com/YOUR_USERNAME/docxtpl_checker.git
cd docxtpl_checker
๐ Quick Start
Basic Usage
# Validate a docxtpl template
python docxtpl_checker.py template.docx
# Generate Chinese report
python docxtpl_checker.py template.docx --language zh --report ้ช่ฏๆฅๅ.md
# Enable verbose logging
python docxtpl_checker.py template.docx --verbose
Example Output
Validation Results:
Errors: 1
Warnings: 0
Info: 2
โ Validation failed with 1 errors
Detailed report saved to: docxtpl_validation_report.md
๐ Usage Guide
Command Line Options
usage: docxtpl_checker.py [-h] [--report REPORT] [--verbose] [--language {en,zh}] template
positional arguments:
template docxtpl template file path (.docx)
options:
-h, --help show this help message and exit
--report REPORT, -r REPORT
Validation report output path (default: docxtpl_validation_report.md)
--verbose, -v Enable verbose logging
--language {en,zh}, -l {en,zh}
Output language: en (English) or zh (Chinese) (default: en)
Examples
# Basic validation
python docxtpl_checker.py my_template.docx
# Custom report location
python docxtpl_checker.py my_template.docx --report reports/validation.md
# Chinese interface and report
python docxtpl_checker.py my_template.docx -l zh -r ไธญๆๆฅๅ.md
# Verbose output for debugging
python docxtpl_checker.py my_template.docx --verbose
๐ What It Validates
Jinja2 Syntax
- Variable tags:
{{ variable }} - Statement tags:
{% if condition %},{% for item in items %} - Comment tags:
{# comment #} - Bracket and quote matching
- Control structure pairing
Docxtpl-specific Features
- Paragraph control:
{% p if condition %}...{% p endif %} - Table row control:
{% tr for item in items %}...{% tr endfor %} - Table cell control:
{% tc if condition %}...{% tc endif %} - Run control:
{% r if condition %}...{% r endif %} - Image filters:
{{ image_var|image }} - Subdocument inclusion:
{{ subdoc(template_var) }}
Common Issues Detected
- โ Unmatched opening/closing tags
- โ Missing docxtpl prefix in end tags
- โ Bracket and parentheses mismatches
- โ Unbalanced quotes
- โ Syntax errors in expressions
- โ Incorrect filter usage
- โ Split tags caused by Word formatting
๐ Report Features
The tool generates detailed markdown reports with:
- ๐ Summary: Overview of errors, warnings, and suggestions
- ๐ฏ Precise Locations: Exact line numbers and document sections
- ๐ Context Display: Shows surrounding lines for easy error location
- ๐ก Helpful Suggestions: Specific recommendations for fixing issues
- ๐ท๏ธ Categorized Issues: Organized by error types and severity
- ๐ Best Practices: Includes docxtpl template guidelines
Sample Report Section
## Errors ๐ด
### Docxtpl Structure
#### 1. {%p if%} tag mismatch: found {%endif%} instead of {%p endif%}
**Location:** Document (Line 9)
**๐ Context:**
7: {% p if false %}
8: {{ name2 }}
9: {% end if %} 10: {% p endif %} 11: {% tr for i in range(10) %}
**๐ก Suggestion:** Change {%endif%} to {%p endif%} to match the docxtpl prefix pattern
๐ Supported Languages
English (Default)
python docxtpl_checker.py template.docx --language en
Chinese (ไธญๆ)
python docxtpl_checker.py template.docx --language zh
All error messages, categories, and suggestions are fully localized.
๐๏ธ Project Structure
docxtpl_checker/
โโโ docxtpl_checker.py # Main validation script
โโโ examples/ # Sample template files
โ โโโ correct.docx # Valid template example
โ โโโ wrong_paragraph.docx # Template with paragraph errors
โ โโโ wrong_tablerow.docx # Template with table row errors
โโโ deprecated/ # Previous versions and experiments
โโโ index.rst # Docxtpl syntax documentation
โโโ README.md # This file
โโโ requirements.txt # Python dependencies
โโโ .gitignore # Git ignore patterns
โโโ LICENSE # MIT license
๐ ๏ธ Development
Running Tests
Test with the provided example files:
# Test correct template
python docxtpl_checker.py examples/correct.docx
# Test problematic templates
python docxtpl_checker.py examples/wrong_paragraph.docx
python docxtpl_checker.py examples/wrong_tablerow.docx
# Test Chinese localization
python docxtpl_checker.py examples/wrong_paragraph.docx --language zh
Adding New Validations
The tool is designed to be extensible. Key methods for adding validations:
validate_single_block(): Add new block-level validationscheck_template_structure(): Add new structure-level validationscheck_docxtpl_specific(): Add docxtpl-specific validations
๐ Requirements
- Python 3.6+
- jinja2
See requirements.txt for specific versions.
๐ค Contributing
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
Areas for Contribution
- Additional validation rules
- More language translations
- Performance improvements
- Additional output formats
- Integration with other tools
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- python-docx-template (docxtpl) - The amazing library this tool validates
- Jinja2 - The template engine used by docxtpl
- python-docx - For Word document processing
๐ Support
If you encounter any issues or have questions:
- Check the examples/ directory for sample usage
- Review the generated validation reports for detailed error information
- Create an issue on GitHub with your template and error details
Happy templating! ๐
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file docxtpl_checker-0.0.1.tar.gz.
File metadata
- Download URL: docxtpl_checker-0.0.1.tar.gz
- Upload date:
- Size: 17.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3cf47c596f70010ced061732739a3ce4d1f177c5894610b6c8daf22dbe3b2b0b
|
|
| MD5 |
41d7524f46931b6749be98afc55de9b1
|
|
| BLAKE2b-256 |
3762bba22f5c97fe3aef17300905dc33ee793edbb32e94051b9054d73613874a
|
File details
Details for the file docxtpl_checker-0.0.1-py3-none-any.whl.
File metadata
- Download URL: docxtpl_checker-0.0.1-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be495fcf29b7bbef59ab0776da045343afc7b5e1390e68e30a8800d641445a9b
|
|
| MD5 |
69ff40fefa2e5e100b7e319e11b0046f
|
|
| BLAKE2b-256 |
1ded2e4e49afc02d0409f20b3db797d5a4bef235368c7207aceffbf5c5c1931a
|