Posted in category Dev Tools on
289 Words ~2 Minute Reading Time • Subscribe to receive updates on Dev Tools
Eric David Smith
Software Engineer / Musician / Entrepreneur
JSON To Word DOCX by Eric David Smith

A simple python script to convert JSON to DOCX.


clone https://github.com/erictherobot/json-to-word.git json-to-docx
cd json-to-docx
pip3 install -r requirements.txt


python3 json-to-docx.py

Once you run this, it will generate a data.docx file in the same directory. You can change the input and output file names in the script. Eventually, I will add command line arguments to make it more flexible.

JSON (input) Example:

    "name": "John",
    "age": 30,
    "city": "New York"
    "name": "Jane",
    "age": 25,
    "city": "Los Angeles"

DOCX (output) Example:

John30New York
Jane25Los Angeles

The Script json-to-docx.py

from docx import Document
import pandas as pd
import json

# Load the JSON data
with open("data.json") as f:
    data = json.load(f)

# Convert the JSON data to a pandas DataFrame
df = pd.DataFrame(data)

# Create a new Word document
doc = Document()

# Add a table to the Word document
table = doc.add_table(rows=1, cols=len(df.columns))

# Set the headers of the table
for i, column in enumerate(df.columns):
    table.cell(0, i).text = column

# Add the data to the table
for index, row in df.iterrows():
    cells = table.add_row().cells
    for i, value in enumerate(row):
        cells[i].text = str(value)

# Save the Word document


Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.



Supporting My Work

Please consider Buying Me A Coffee. I work hard to bring you my best content and any support would be greatly appreciated. Thank you for your support!


Eric David Smith
Software Engineer / Musician / Entrepreneur
Dev Tools

Related Blog Posts

Blog Post Tags