Skip to main content

Python implementation of the MDOCX (MarkDown Open Container eXchange) file format

Project description

PyMDOCX

Python implementation of the MDOCX (MarkDown Open Container eXchange) file format.

MDOCX is a single-file container format for bundling one or more Markdown documents with associated binary media (images, audio, video, etc.), suitable for exchange, archival, and transport.

Installation

pip install logicossoftware-mdocx

Optional compression support

# Install with all compression algorithms
pip install logicossoftware-mdocx[all]

# Or install individual compression libraries
pip install logicossoftware-mdocx[zstd]   # Zstandard (recommended)
pip install logicossoftware-mdocx[lz4]    # LZ4 (fast)
pip install logicossoftware-mdocx[brotli] # Brotli (high compression)

Quick Start

Creating an MDOCX file

from pymdocx import (
    MDOCXWriter,
    MarkdownBundle,
    MarkdownFile,
    MediaBundle,
    MediaItem,
    Metadata,
    CompressionMethod,
)

# Create markdown content
md_bundle = MarkdownBundle(
    root_path="index.md",
    files=[
        MarkdownFile.from_string(
            "index.md",
            "# My Document\n\n![Logo](mdocx://media/logo)\n\nWelcome!"
        ),
        MarkdownFile.from_string(
            "docs/chapter1.md",
            "# Chapter 1\n\nThis is the first chapter."
        ),
    ],
)

# Add media (optional)
with open("logo.png", "rb") as f:
    logo_data = f.read()

media_bundle = MediaBundle(items=[
    MediaItem(
        id="logo",
        path="assets/logo.png",
        mime_type="image/png",
        data=logo_data,
    ),
])

# Add metadata (optional)
metadata = Metadata(
    title="My Document",
    creator="Author Name",
    root="index.md",
    tags=["example", "documentation"],
)

# Write the file
writer = MDOCXWriter(compression=CompressionMethod.ZSTD)
writer.write("document.mdocx", md_bundle, media_bundle, metadata)

Reading an MDOCX file

from pymdocx import MDOCXReader

reader = MDOCXReader()
doc = reader.read("document.mdocx")

# Access metadata
if doc.metadata:
    print(f"Title: {doc.metadata.title}")
    print(f"Creator: {doc.metadata.creator}")

# Access markdown files
for md_file in doc.markdown_bundle.files:
    print(f"File: {md_file.path}")
    print(f"Content: {md_file.content_str[:100]}...")

# Get the root/primary file
root_file = doc.get_root_file()
if root_file:
    print(f"Root file: {root_file.path}")

# Access media
for item in doc.media_bundle.items:
    print(f"Media: {item.id} ({item.mime_type})")

# Resolve media URIs from markdown
media = doc.resolve_media_uri("mdocx://media/logo")
if media:
    print(f"Found media: {len(media.data)} bytes")

# List contents (paths, sizes, metadata)
contents = doc.list_contents()
print("Root:", contents["root"])
for item in contents["markdown"]:
    print("MD:", item["path"], item["size"])
for item in contents["media"]:
    print("Media:", item["id"], item["path"], item["size"])

Compression Options

Method Description Use Case
CompressionMethod.NONE No compression Maximum compatibility
CompressionMethod.ZIP ZIP/DEFLATE Good interoperability
CompressionMethod.ZSTD Zstandard Recommended - best speed/ratio
CompressionMethod.LZ4 LZ4 Fastest compression/decompression
CompressionMethod.BROTLI Brotli Maximum compression ratio
# Use different compression for markdown and media
writer = MDOCXWriter(
    markdown_compression=CompressionMethod.ZSTD,
    media_compression=CompressionMethod.LZ4,
)

API Reference

Models

  • MarkdownBundle: Collection of Markdown files
  • MarkdownFile: Single Markdown document with path and content
  • MediaBundle: Collection of media items
  • MediaItem: Binary media with ID, path, MIME type, and data
  • Metadata: Document metadata (title, creator, tags, etc.)

Reader/Writer

  • MDOCXReader: Read and parse MDOCX files
  • MDOCXWriter: Create MDOCX files

MDOCXReader Options

reader = MDOCXReader(
    max_metadata_length=1024*1024,      # 1 MiB
    max_markdown_uncompressed=256*1024*1024,  # 256 MiB
    max_media_uncompressed=2*1024*1024*1024,  # 2 GiB
    verify_hashes=True,  # Verify SHA256 hashes on media
)

File Format

MDOCX files consist of:

  1. Fixed Header (32 bytes): Magic, version, flags
  2. Metadata Block (optional): UTF-8 JSON
  3. Markdown Section: Bundled Markdown files
  4. Media Section: Bundled binary media

See rfc.md for the complete specification.

Development

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest tests/ -v

License

MIT License Python implementation of MDOCX file format

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

logicossoftware_mdocx-0.1.0.tar.gz (42.4 kB view details)

Uploaded Source

Built Distribution

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

logicossoftware_mdocx-0.1.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for logicossoftware_mdocx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d8646d15ef776d061da89d6820d50d7aa1b7c812a9384ddca6521b768abec8ed
MD5 5edc185941527bc9ff17a24a7dff4c77
BLAKE2b-256 621ceb9b3bf513d640c39b074cdd4562af12054332baa4185f0855cba29b41a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for logicossoftware_mdocx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0be017b8ca006f2d0a613537f12dd47bb599040f66417f98aa4d508c77d1915
MD5 5fa517cf31502198613c77e579b47b3f
BLAKE2b-256 bbf31a7adb9d9e47ef9a3738a48ac12251c350be02d9de2ec8501aea00d7298a

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