Skip to main content

Pure-Rust Word (.docx / OOXML) structural parser with strong table support and local image OCR, via PyO3.

Project description

docspine

PyPI

A pure-Rust Word (.docx) parser with Python bindings (PyO3 / maturin, abi3-py311). A .docx file is OOXML — a zip archive of XML parts — and docspine walks word/document.xml directly to produce a structured, information-preserving model: paragraphs (styled runs), tables (rows, cells, merges, fills, nesting), and embedded pictures. Tables are a first-class focus. Embedded images can additionally be OCR'd locally, offline, and deterministically via the sibling ocrspine crate (PP-OCRv5 through tract-onnx — no cloud, no network), and an image that is a table can be reconstructed into a grid from its OCR word boxes.

docspine is the document-engine sibling of pdfspine (PDF) and pptspine (PowerPoint), all sharing the same ocrspine OCR core.

Capabilities

Area Status
Body blocks: paragraphs + tables in document order parsed
Paragraphs: runs, text, style name, alignment, list level parsed
Run styling: font, size, bold, italic, underline, color parsed
Tables: rows, cells, cell paragraphs parsed
Table merges: gridSpan (horizontal) parsed
Table merges: vMerge restart / continue (vertical) parsed
Nested tables (a table inside a cell) parsed
Cell shading/fill, cell width (dxa), table grid columns parsed
Row height, header rows parsed
Embedded pictures: r:embed rel → media name + raw bytes + EMU extent parsed
Image OCR (embedded pictures → words + boxes) working (ocr_image)
Image-table reconstruction from OCR boxes → grid working (reconstruct_image_table)
Legacy binary .doc (OLE/CFB) probe + typed downgrade (full body deferred)

Parsing is tolerant: unknown elements are skipped, missing attributes become None, and malformed input yields a typed DocError rather than a panic.

docx first; legacy .doc deferred

Modern .docx (OOXML) is the primary target. The old binary .doc is a Microsoft compound document (OLE/CFB): rebuilding its body from the binary FIB + piece table is large, fiddly, and shares almost nothing with the docx path. So docspine ships detection + a clean typed downgrade today (a .doc byte stream yields DocUnsupportedError, and probe_doc reports the CFB streams when built with the legacy-doc feature); full .doc body reconstruction is a follow-up, not a blocker.

Install

pip install docspine

docspine is on PyPI. OCR works out of the box: the PP-OCRv5 weights ship in the shared ocrspine-models data package — a runtime dependency pip pulls in automatically — so a plain pip install docspine finds the OCR models with no extra setup (it no longer needs a sibling ../ocrspine/models checkout or OCRSPINE_MODELS). To build from source instead, see below.

Build (from the package root)

uv venv .venv
VIRTUAL_ENV="$(pwd)/.venv" uv pip install maturin pytest
# Structural parsing needs no models. The OCR path resolves models from a
# sibling ../ocrspine/models by default (or set OCRSPINE_MODELS).
OCRSPINE_MODELS="$(cd ../ocrspine && pwd)/models" \
  VIRTUAL_ENV="$(pwd)/.venv" .venv/bin/maturin develop --release

Use from Python

import docspine

doc = docspine.open("report.docx")
print(doc.block_count)

for block in doc.body():            # list[dict], introspectable
    if block["kind"] == "paragraph":
        for run in block["runs"]:
            print(run["text"], run["bold"], run["color"])
    elif block["kind"] == "table":
        for row in block["rows"]:
            for cell in row["cells"]:
                print(cell["text"], "span", cell["grid_span"], cell["v_merge"])

# Run OCR on raw image bytes (PNG/JPEG), offline:
items = docspine.ocr_image(open("scan.png", "rb").read())
print(" ".join(i["text"] for i in items))

# Reconstruct a table that lives inside an image into a grid:
for table in docspine.reconstruct_image_table(open("table.png", "rb").read()):
    for cell in table["cells"]:
        print(cell["row"], cell["col"], cell["text"])

Rust workspace

crates/
  doc-core    domain model + geometry (twip/EMU) + typed DocError. No IO/zip/XML.
  doc-parse   OOXML reader: zip extract + quick-xml walk -> Document.
              Legacy binary .doc probing behind the `legacy-doc` feature.
  doc-ocr     image-OCR bridge over ocrspine (PaddleOcr) + image-table reconstruction.
  py-bindings PyO3 _core extension (the FFI chokepoint); `ocr` feature gates OCR.

Deferred / follow-up

  • Full legacy binary .doc (OLE/CFB / [MS-DOC]) body reconstruction (FIB, piece table, CHPX/PAPX). Today: detection + typed downgrade + probe_doc.
  • Richer styling (theme colors, styles.xml inheritance), headers/footers, footnotes/endnotes, comments, fields, hyperlinks targets, SmartArt/charts.

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

docspine-0.1.1.tar.gz (61.5 kB view details)

Uploaded Source

Built Distributions

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

docspine-0.1.1-cp311-abi3-win_amd64.whl (6.1 MB view details)

Uploaded CPython 3.11+Windows x86-64

docspine-0.1.1-cp311-abi3-manylinux_2_28_aarch64.whl (5.2 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.28+ ARM64

docspine-0.1.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.1 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64

docspine-0.1.1-cp311-abi3-macosx_11_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

docspine-0.1.1-cp311-abi3-macosx_10_12_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.11+macOS 10.12+ x86-64

File details

Details for the file docspine-0.1.1.tar.gz.

File metadata

  • Download URL: docspine-0.1.1.tar.gz
  • Upload date:
  • Size: 61.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for docspine-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6623d8ba9895b4bd45604fbe2b05e8c5938585955727b593c64c82b10c9e3bab
MD5 4bb5cd32f9672011d190f088f287ec1c
BLAKE2b-256 684a90ea532b614b6bae0d012e9a931381f2df07a76b5c63c571dd77bcfde4fd

See more details on using hashes here.

File details

Details for the file docspine-0.1.1-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: docspine-0.1.1-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 6.1 MB
  • Tags: CPython 3.11+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for docspine-0.1.1-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2fce501f6a1ec5d0d6a180704a3137ea0966db0bfae5811a93ac62ec4004329c
MD5 d3701a84627033ff347959a45a797a65
BLAKE2b-256 553cfdf3ec4f9a0c0f13bd82afb8360647c4bd2a2959e5ed9c551f17f30aad06

See more details on using hashes here.

File details

Details for the file docspine-0.1.1-cp311-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for docspine-0.1.1-cp311-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 1a75b7e7e69340b069399309a6c7f92fc799cfeeb8b330fc2303462ca62d9a11
MD5 4c60887fa8dad56e251498028a7f464d
BLAKE2b-256 55556df50827430f379b90b2a068729a60a2869c117e0a9803a341cec8e582d4

See more details on using hashes here.

File details

Details for the file docspine-0.1.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for docspine-0.1.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 39c04b0b7be522568b5ebbf79f7bb12a3da947e46935bad76fc66f99e8ebc994
MD5 d4089181b447de1957dec279a88ffd69
BLAKE2b-256 63eed505a1b71d82d087e3a26991d27aa0b94020c9bd12df2a515bc070bc0e08

See more details on using hashes here.

File details

Details for the file docspine-0.1.1-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for docspine-0.1.1-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d4cb4b8156f95eae6b22e842a87cc164dd88b45def02d8909d166b3bc970c927
MD5 3ea6debcc5bdac2b35310b486b3ff6d4
BLAKE2b-256 687ef40f9e63dbc30b2bb6ad967fe67a7a066b44c8fc15b001b56e71aee0454f

See more details on using hashes here.

File details

Details for the file docspine-0.1.1-cp311-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for docspine-0.1.1-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 bdd1e46326db1f71077c9fdd9b29c17d94bd1db3fa9172ea236d570a3ea51f22
MD5 29ea1e6b4b21133ee8a681efe34853ef
BLAKE2b-256 9c1f9381bb57364219d8afd52832deaa94109b28e930311bc256ce88c3c083a5

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