Replace template text in DOCX files with values from JSON
Project description
docx-json-replacer
Replace template placeholders in DOCX files with JSON data. Supports text replacement in paragraphs, table cells, and dynamic table generation.
Features
- Replace
{{placeholder}}markers in DOCX files with JSON data - Handles placeholders in paragraphs AND table cells
- Supports keys with dots (e.g.,
part.borrower_name) - Automatic HTML tag cleaning
- Dynamic table generation from JSON arrays
- Table styling support (colors, bold, italic)
Installation
pip install docx-json-replacer
Usage
Command Line
# Basic usage
docx-json-replacer template.docx data.json -o output.docx
# Without -o flag, creates template_replaced.docx
docx-json-replacer template.docx data.json
Python API
from docx_json_replacer import DocxReplacer
# Create replacer instance
replacer = DocxReplacer('template.docx')
# Replace with JSON data
json_data = {
"customer_name": "John Doe",
"date": "2024-01-01",
"amount": "$1,000.00"
}
replacer.replace_from_json(json_data)
# Save the result
replacer.save('output.docx')
Template Format
In your DOCX file, use double curly braces for placeholders:
Dear {{customer_name}},
Your invoice dated {{date}} for {{amount}} is ready.
Placeholders work in:
- Regular paragraphs
- Table cells
- Headers and footers
JSON Data Format
Simple Text Replacement
{
"customer_name": "John Doe",
"date": "2024-01-01",
"amount": "$1,000.00"
}
Keys with Dots
{
"part.borrower_name": "ACME Corp",
"part.borrower_address": "123 Main St"
}
Table Data
For dynamic table generation, use arrays:
{
"invoice_items": [
{"cells": ["Item 1", "2", "$50.00"]},
{"cells": ["Item 2", "3", "$75.00"]}
]
}
With styling:
{
"styled_table": [
{
"cells": ["Header 1", "Header 2"],
"style": {"bg": "4472C4", "color": "FFFFFF", "bold": true}
},
{
"cells": ["Data 1", "Data 2"]
}
]
}
Complete Example
Template (template.docx)
Contract Number: {{contract_number}}
Client: {{client.name}}
Address: {{client.address}}
Items:
{{items}}
Data (data.json)
{
"contract_number": "2024-001",
"client.name": "ABC Corporation",
"client.address": "456 Business Ave",
"items": [
{"cells": ["Product", "Qty", "Price"], "style": {"bold": true}},
{"cells": ["Widget A", "10", "$100"]},
{"cells": ["Widget B", "5", "$200"]}
]
}
Command
docx-json-replacer template.docx data.json -o contract_2024_001.docx
Requirements
- Python 3.7+
- python-docx
- docxtpl
License
MIT
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 docx_json_replacer-0.5.1.tar.gz.
File metadata
- Download URL: docx_json_replacer-0.5.1.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b37034eabc1291df524ae39bc0600c6e0a51d8766e8b43b4a2c0333e333cf2f
|
|
| MD5 |
b6307211a619b923fbdda9318b851165
|
|
| BLAKE2b-256 |
903563097202a582ec3a9c843abb704daccb62ea62b390cddda65de46265d957
|
File details
Details for the file docx_json_replacer-0.5.1-py3-none-any.whl.
File metadata
- Download URL: docx_json_replacer-0.5.1-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fca10535f17c5bc36ed4ec0b670d8958d0a83b109b67ee864d38ca356167d112
|
|
| MD5 |
b7f22461fc9ba2cc3aad1faae765116a
|
|
| BLAKE2b-256 |
2f4793641686f1411ebdad9bd4b32f4780a452351a9f66c0803c3bf905b4e24e
|