A powerful Word document style generator with TOML configuration support, designed for Chinese/English mixed documents.
- TOML Configuration: Define styles through config files, no code changes needed
- Chinese/English Font Support: Perfect handling of mixed Chinese/English documents
- Content Preservation: Modify styles while preserving all original content
- Style Inheritance: Support for style creation, modification and inheritance
- Table Styles: Custom table borders, margins and other styles
- Hanging Indent: Full support for Word hanging indent functionality
- Preset Templates: Multiple predefined style templates
# Install dependencies
pip install python-docx toml
# Development installation (recommended)
pip install -e .After installation, use the docx-template command:
# Use preset configurations
docx-template -c common_styles.toml
docx-template -c contract_styles.toml
docx-template -c report_styles.toml
docx-template -c traditional_styles.toml
# Specify custom input/output
docx-template -c configs/common_styles.toml -i my_template.docx -o my_output.docx
# Use absolute paths
docx-template -c /path/to/config.toml -i /path/to/input.docx -o /path/to/output.docx# View help
docx-template --help
# List available configurations
docx-template --list-configs
# Create sample configuration
docx-template --create-sample-config my_config.toml| Configuration | Description | Font | Features |
|---|---|---|---|
common_styles.toml |
General styles | Microsoft YaHei | Table styles, tab stops |
contract_styles.toml |
Contract styles | Microsoft YaHei | Hanging indent, justified |
report_styles.toml |
Report styles | Microsoft YaHei | Complex heading hierarchy |
traditional_styles.toml |
Traditional styles | FangSong_GB2312 etc | Traditional official fonts |
[meta]
name = "My Style Configuration"
description = "Custom style description"
version = "1.0.0"
template_path = "templates/base.docx" # Input template path
output_path = "outputs/styled.docx" # Output path
[styles.Normal]
chinese_font = "Microsoft YaHei"
english_font = "Microsoft YaHei"
size_pt = 11
bold = false
alignment = "justify"
first_line_indent = "0.74cm"
[styles.Title]
chinese_font = "Microsoft YaHei"
english_font = "Microsoft YaHei"
size_pt = 18
bold = true
alignment = "center"
[table]
enabled = true
style_name = "Table"chinese_font: Chinese font nameenglish_font: English font namesize_pt: Font size in pointsbold: Bold formatting (true/false)italic: Italic formatting (true/false)
alignment: Alignment ("left", "center", "right", "justify")space_before: Space before paragraphspace_after: Space after paragraphfirst_line_indent: First line indentleft_indent: Left indenthanging_indent: Hanging indent
tab_stops: Tab stop list, e.g.["4.5cm", "9cm", "13.5cm"]
cm: Centimeters (default)pt: Pointsin: Inchesmm: Millimetersnone: Clear setting
templates/directory- Relative path (relative to current working directory)
- Absolute path
- Command line arguments have highest priority
- Then configuration file
output_path - Finally auto-generated filename
project/
├── configs/
│ └── my_style.toml
├── templates/
│ └── base.docx
└── outputs/
└── styled.docx
# Apply unified styles to company templates
docx-template -c configs/company_standard.toml -i templates/company_base.docx -o outputs/company_template.docx# Contract documents
docx-template -c contract_styles.toml -i contracts/template.docx -o contracts/styled_contract.docx
# Report documents
docx-template -c report_styles.toml -i reports/template.docx -o reports/styled_report.docx#!/bin/bash
for doc_type in contract report traditional; do
docx-template -c ${doc_type}_styles.toml \
-i templates/base.docx \
-o outputs/${doc_type}_template.docx
donedocx_template_generator/
├── __init__.py # Package initialization
├── core.py # Core style processing
├── config.py # TOML configuration loading
├── table_styles.py # Table style processing
└── cli.py # Command line interface
- StyleApplicator: Style applicator for specific style settings
- DocumentProcessor: Document processor for loading, processing, saving
- ConfigLoader: Configuration loader for parsing TOML files
- TableStyler: Table style processor for table style settings
from docx_template_generator import DocumentProcessor, ConfigLoader
# Load configuration
config_loader = ConfigLoader("configs/my_style.toml")
config_loader.load_config()
# Process document
processor = DocumentProcessor("input.docx", "output.docx")
processor.process_with_config(
config_loader.get_styles_config(),
config_loader.get_table_config()
)- Windows Only: This project only supports Windows environment
- Font Dependencies: Ensure fonts specified in configuration are installed
- Content Preservation: Tool preserves all input document content, only modifies style definitions
- Path Format: Use forward slashes
/or double backslashes\\in config files
Individual Python scripts have been replaced by unified CLI tool:
# Old way
python common_styles.py
# New way
docx-template -c common_styles.toml- Create new TOML configuration file
- Define style properties and paths
- Test with
docx-templatecommand
- Inherit core classes for extension
- Add new style property handling
- Extend configuration format support
This project is licensed under the MIT License.