Skip to main content

Convert .docx files to structured JSON and back.

Project description

docx_json_converter

A robust Python package for converting .docx files into structured JSON and back again. Designed for AI agents that need to read or generate Microsoft Word documents with high fidelity.

Features

  • Round-Trip Formatting: Preserves Bold, Italic, Underline, and Font Size.
  • Run-Level Precision: Handles mixed formatting within paragraphs (e.g., "Hello World").
  • Column Support: Detects and applies document-wide column layouts (e.g., 2-column newsletters).
  • Structure: Supports Paragraphs, Bullet Lists, and Headers.
  • AI-Ready: Generates optimized JSON schemas for LLMs.

Installation

pip install docx_json_converter

(Or install locally for development):

pip install -e .

Quick Start

1. Python API

from docx_json_converter import convert_docx_to_json, convert_json_to_docx

# Convert DOCX -> JSON
data = convert_docx_to_json("input.docx")
# data = {"metadata": {"columns": 1}, "blocks": [...]}

# Convert JSON -> DOCX
convert_json_to_docx(data, "output.docx")

Simplified Mode (Text Only):

# Returns blocks without detailed formatting/runs
data = convert_docx_to_json("input.docx", include_formatting=False)

2. CLI for AI Prompts

To teach an LLM (like ChatGPT or Claude) how to generate documents using this package, generate the system prompt file:

docx-json-prompt

This creates llm.txt with the JSON schema and rules.

The Zen of docx_json_converter

import docx_json_converter
docx_json_converter.zen()

JSON Schema Example

{
  "metadata": {
    "columns": 2
  },
  "blocks": [
    {
      "text": "My Title",
      "block_type": "title",
      "formatting": { "alignment": "center" },
      "runs": [
        { "text": "My Title", "formatting": { "bold": true, "font_size": 16.0 } }
      ]
    },
    {
      "text": "Bullet point 1",
      "block_type": "list_item",
      "formatting": { "alignment": "left" }
    }
  ]
}

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docx_json_converter-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docx_json_converter-0.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file docx_json_converter-0.1.0.tar.gz.

File metadata

  • Download URL: docx_json_converter-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for docx_json_converter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 095ef30866a7e0df0e9e8a2b1357a46df64d6ba67379e1b4da6e80283c8f0693
MD5 de389cbf57016a59cd303d80113304ef
BLAKE2b-256 b95add8338070b7c1da6b8c45a7cdac5fd8d045b8dc8b456220ae13d7a9273ca

See more details on using hashes here.

File details

Details for the file docx_json_converter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for docx_json_converter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad3b712e9e74dc5a59d8e3502f947afaf43307d1066744d5036cab2fb50623a8
MD5 abefbbcb0e2ca467c3dbd4df7f3dbfab
BLAKE2b-256 e42af10c96ab689f2ae364d1a2190d76e0b741cdf2709a54c8cde4b43fa8c7f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page