Skip to content

PyBalance/docx-template-generator

Repository files navigation

DOCX Template Generator

A powerful Word document style generator with TOML configuration support, designed for Chinese/English mixed documents.

Features

  • 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

Installation

# Install dependencies
pip install python-docx toml

# Development installation (recommended)
pip install -e .

Quick Start

Basic Usage

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

Help Commands

# View help
docx-template --help

# List available configurations
docx-template --list-configs

# Create sample configuration
docx-template --create-sample-config my_config.toml

Preset Configurations

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

Configuration Format

Basic Structure

[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"

Supported Style Properties

Font Properties

  • chinese_font: Chinese font name
  • english_font: English font name
  • size_pt: Font size in points
  • bold: Bold formatting (true/false)
  • italic: Italic formatting (true/false)

Paragraph Properties

  • alignment: Alignment ("left", "center", "right", "justify")
  • space_before: Space before paragraph
  • space_after: Space after paragraph
  • first_line_indent: First line indent
  • left_indent: Left indent
  • hanging_indent: Hanging indent

Tab Stops

  • tab_stops: Tab stop list, e.g. ["4.5cm", "9cm", "13.5cm"]

Length Units

  • cm: Centimeters (default)
  • pt: Points
  • in: Inches
  • mm: Millimeters
  • none: Clear setting

Path Resolution

Template File Search Priority

  1. templates/ directory
  2. Relative path (relative to current working directory)
  3. Absolute path

Output File

  • Command line arguments have highest priority
  • Then configuration file output_path
  • Finally auto-generated filename

Example Directory Structure

project/
├── configs/
│   └── my_style.toml
├── templates/
│   └── base.docx
└── outputs/
    └── styled.docx

Usage Scenarios

Scenario 1: Company Document Standardization

# Apply unified styles to company templates
docx-template -c configs/company_standard.toml -i templates/company_base.docx -o outputs/company_template.docx

Scenario 2: Different Document Types

# 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

Scenario 3: Batch Processing

#!/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
done

Architecture

Package Structure

docx_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

Core Classes

  • 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

Programming Interface

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()
)

Important Notes

  • 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

Migration from Old Version

Individual Python scripts have been replaced by unified CLI tool:

# Old way
python common_styles.py

# New way
docx-template -c common_styles.toml

Development Guide

Adding New Configuration

  1. Create new TOML configuration file
  2. Define style properties and paths
  3. Test with docx-template command

Extending Functionality

  • Inherit core classes for extension
  • Add new style property handling
  • Extend configuration format support

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages