Replace template text in DOCX files with values from JSON
Project description
DOCX JSON Replacer
A powerful Python library for replacing template placeholders in DOCX files with JSON data. Supports advanced features like dynamic tables, HTML formatting in cells, and individual cell styling.
✨ Features
- 📝 Simple placeholder replacement in paragraphs and tables
- 📊 Dynamic table generation from JSON data
- 🎨 Advanced table styling with row-level and cell-level customization
- 🔤 HTML formatting support in table cells (
<b>,<i>,<u>,<br>,<p>) - 🎯 Smart HTML tag handling for malformed or duplicate tags
- 🚀 Batch processing capabilities
- 💻 Command-line interface for easy automation
- 🐍 Simple Python API for integration
📦 Installation
pip install docx-json-replacer
🚀 Quick Start
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 = {
"name": "John Doe",
"company": "Acme Corp",
"table_data": [
{
"cells": ["Header 1", "Header 2", "Header 3"],
"style": {"bg": "4472C4", "color": "FFFFFF", "bold": True}
},
{
"cells": ["Row 1 Col 1", "Row 1 Col 2", "Row 1 Col 3"]
}
]
}
replacer.replace_from_json(json_data)
replacer.save('output.docx')
📄 Template Format
Use double curly braces for placeholders:
Dear {{name}},
Welcome to {{company}}!
{{table_data}}
Placeholders work in:
- Regular paragraphs
- Table cells
- Headers and footers
- Nested structures with dots (e.g.,
{{client.name}})
📊 Table Support
Basic Table (List of Lists)
{
"simple_table": [
["Header 1", "Header 2"],
["Row 1 Col 1", "Row 1 Col 2"]
]
}
Styled Table with Row-Level Styling
{
"styled_table": [
{
"cells": ["Header 1", "Header 2", "Header 3"],
"style": {
"bg": "4472C4",
"color": "FFFFFF",
"bold": true
}
},
{
"cells": ["Data 1", "Data 2", "Data 3"],
"style": {
"bg": "F2F2F2"
}
}
]
}
Individual Cell Styling (v0.6.0+)
{
"cell_styled_table": [
{
"cells": ["Red Cell", "Green Cell", "Blue Cell"],
"cell_styles": [
{"bg": "FF0000", "color": "FFFFFF", "bold": true},
{"bg": "00FF00", "color": "000000", "italic": true},
{"bg": "0000FF", "color": "FFFFFF", "underline": true}
]
}
]
}
Mixed Row and Cell Styling
{
"mixed_table": [
{
"cells": ["Default", "Default", "Special"],
"style": {"bg": "E7E6E6"},
"cell_styles": [
null,
null,
{"bg": "FFFF00", "bold": true}
]
}
]
}
HTML Formatting in Cells (v0.6.0+)
{
"html_table": [
{
"cells": [
"Normal text",
"<b>Bold text</b>",
"<i>Italic</i> and <u>underline</u>"
]
},
{
"cells": [
"Line 1<br>Line 2<br>Line 3",
"<b>Title</b><br><i>Subtitle</i>",
"<p>Paragraph 1</p><p>Paragraph 2</p>"
]
}
]
}
🎨 Style Properties
| Property | Description | Example |
|---|---|---|
bg |
Background color (hex without #) | "4472C4" |
color |
Text color (hex without #) | "FFFFFF" |
bold |
Bold text | true/false |
italic |
Italic text | true/false |
underline |
Underlined text | true/false |
Style Priority Order
- Inline cell object style (highest priority)
cell_stylesarray entry- Row
style(lowest priority)
🔧 Advanced Usage
Processing Multiple Files
from docx_json_replacer import DocxReplacer
import json
# Process multiple documents
templates = ['template1.docx', 'template2.docx']
data_files = ['data1.json', 'data2.json']
for template, data_file in zip(templates, data_files):
with open(data_file, 'r') as f:
data = json.load(f)
replacer = DocxReplacer(template)
replacer.replace_from_json(data)
replacer.save(f'output_{template}')
Real-World Example: Invoice Generation
from docx_json_replacer import DocxReplacer
invoice_data = {
"invoice_number": "INV-2024-001",
"date": "2024-01-15",
"client.name": "ABC Corporation",
"client.address": "123 Business St.",
"items": [
{
"cells": ["Item", "Quantity", "Price", "Total"],
"style": {"bg": "333333", "color": "FFFFFF", "bold": True}
},
{
"cells": ["Widget A", "10", "$10.00", "$100.00"]
},
{
"cells": ["Widget B", "5", "$20.00", "$100.00"]
},
{
"cells": ["<b>Total</b>", "", "", "<b>$200.00</b>"],
"cell_styles": [
{"bg": "E7E6E6", "bold": True},
{"bg": "E7E6E6"},
{"bg": "E7E6E6"},
{"bg": "E7E6E6", "bold": True}
]
}
]
}
replacer = DocxReplacer('invoice_template.docx')
replacer.replace_from_json(invoice_data)
replacer.save('invoice_INV-2024-001.docx')
📋 Complete Example
Template (template.docx)
Contract Number: {{contract_number}}
Client: {{client.name}}
Address: {{client.address}}
Items:
{{items}}
Terms: {{terms}}
Data (data.json)
{
"contract_number": "2024-001",
"client.name": "ABC Corporation",
"client.address": "456 Business Ave",
"items": [
{
"cells": ["Product", "Quantity", "Price"],
"style": {"bg": "4472C4", "color": "FFFFFF", "bold": true}
},
{
"cells": ["<b>Widget A</b>", "10", "$100"],
"cell_styles": [{"bg": "E7E6E6"}, null, null]
},
{
"cells": ["<b>Widget B</b>", "5", "$200"],
"cell_styles": [{"bg": "E7E6E6"}, null, null]
}
],
"terms": "Payment due in 30 days"
}
Command
docx-json-replacer template.docx data.json -o contract_2024_001.docx
🆕 What's New in v0.6.0
- HTML Support in Tables: Format text with
<b>,<i>,<u>,<br>, and<p>tags - Cell-Level Styling: Individual styling for each cell in a table
- Smart Tag Handling: Properly handles malformed or duplicate HTML tags
- Improved Performance: Optimized table generation and styling
📋 Requirements
- Python 3.7+
- python-docx >= 0.8.11
- docxcompose >= 1.3.0
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Built with python-docx
- Table composition with docxcompose
📞 Support
For issues and feature requests, please use the GitHub issue tracker.
📚 Links
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.6.4.tar.gz.
File metadata
- Download URL: docx_json_replacer-0.6.4.tar.gz
- Upload date:
- Size: 20.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40b270dbecf21a1042fb19b84d64872032580896c34b394d4aaa636d94f0727d
|
|
| MD5 |
79dcf68353b99b5647b6475c1490dc9a
|
|
| BLAKE2b-256 |
0aeb3535fa519d59f390fc337669f8b13dc2462ffee9b7fc3f2379c086c510bf
|
File details
Details for the file docx_json_replacer-0.6.4-py3-none-any.whl.
File metadata
- Download URL: docx_json_replacer-0.6.4-py3-none-any.whl
- Upload date:
- Size: 20.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 |
4addfc3517f1053680cad0259373278a2d102d0e396dc09524413d568aeff2d9
|
|
| MD5 |
bac0e3a4b549f74bc558dca15c06c521
|
|
| BLAKE2b-256 |
42be23abd48af5f909ea4f9c3df17b5be30e2ebb9e45263855895700df1c0bf1
|