Skip to main content

A comprehensive validation tool for python-docx-template (docxtpl) Word templates

Project description

Docxtpl Template Checker

Python License

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
  • jinja2 library

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 validations
  • check_template_structure(): Add new structure-level validations
  • check_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

๐Ÿ“ž Support

If you encounter any issues or have questions:

  1. Check the examples/ directory for sample usage
  2. Review the generated validation reports for detailed error information
  3. Create an issue on GitHub with your template and error details

Happy templating! ๐ŸŽ‰

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docxtpl_checker-0.0.1.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docxtpl_checker-0.0.1-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

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

Hashes for docxtpl_checker-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3cf47c596f70010ced061732739a3ce4d1f177c5894610b6c8daf22dbe3b2b0b
MD5 41d7524f46931b6749be98afc55de9b1
BLAKE2b-256 3762bba22f5c97fe3aef17300905dc33ee793edbb32e94051b9054d73613874a

See more details on using hashes here.

File details

Details for the file docxtpl_checker-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for docxtpl_checker-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 be495fcf29b7bbef59ab0776da045343afc7b5e1390e68e30a8800d641445a9b
MD5 69ff40fefa2e5e100b7e319e11b0046f
BLAKE2b-256 1ded2e4e49afc02d0409f20b3db797d5a4bef235368c7207aceffbf5c5c1931a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page