Skip to content

DOCX Document Stamping

PrimeStamp supports Microsoft Word DOCX files with full content hashing, metadata extraction, and structural fingerprinting. Because DOCX is a ZIP-based format containing XML, PrimeStamp can provide both whole-document and per-section authentication.

Quick Start

from primestamp.integrations.docx_extract import DocxExtractor, PrimeStampDocx

extractor = DocxExtractor()

# Extract from file path
result = extractor.extract("report.docx")

# Or from bytes
result = extractor.extract_from_bytes(docx_bytes)

print(f"Paragraphs: {result.paragraph_count}")
print(f"Words: {result.word_count}")
print(f"Headings: {result.heading_count}")
print(f"Tables: {result.table_count}")
print(f"Content hash: {result.content_hash}")

integration = PrimeStampDocx(extractor)
stamped = await integration.stamp_docx(docx_bytes, "report.docx", preset="standard")

Dependencies

pip install python-docx
# or install with the integrations extra:
pip install "primestamp[integrations]"

Features

  • Paragraph-level extraction with style detection
  • Heading hierarchy reconstruction (H1-H9)
  • Table extraction with row/column hashing
  • Image reference cataloging
  • Header/footer content capture
  • Comment and revision tracking
  • Footnote and endnote extraction
  • Section-level Merkle tree for partial proofs
  • Metadata extraction (author, dates, custom properties)

Structural Elements

Element Description
heading Headings with level (1-9) from Word styles
paragraph Body text paragraphs
list_item Bulleted or numbered list items
table Tables with cell-level content
image Embedded image references
header Page header content
footer Page footer content
footnote Footnotes
endnote Endnotes
comment Review comments with author and date
for element in result.elements:
    print(f"[{element.element_type}] {element.content[:80]}")
    if element.heading_level:
        print(f"  Level: {element.heading_level}")
    if element.style_name:
        print(f"  Style: {element.style_name}")

Heading Hierarchy

PrimeStamp reconstructs the full heading hierarchy from Word's named styles:

for element in result.elements:
    if element.element_type == "heading":
        indent = "  " * (element.heading_level - 1)
        print(f"{indent}H{element.heading_level}: {element.content}")

Section-Level Merkle Tree

Each structural element gets its own hash, combined into a Merkle tree for partial proofs. This enables proving a specific clause exists in a contract without revealing the rest:

proof = result.generate_section_proof(section_index=5)

Metadata Extraction

metadata = result.metadata
print(f"Title: {metadata.get('title')}")
print(f"Author: {metadata.get('author')}")
print(f"Last modified by: {metadata.get('last_modified_by')}")
print(f"Created: {metadata.get('created')}")
print(f"Modified: {metadata.get('modified')}")
print(f"Revision: {metadata.get('revision')}")

Comments and Revisions

for comment in result.comments:
    print(f"Comment by {comment.author} at {comment.date}: {comment.text}")

for revision in result.revisions:
    print(f"Revision by {revision.author}: {revision.type}")

Table Extraction

for element in result.elements:
    if element.element_type == "table":
        print(f"Table with {element.metadata.get('rows')} rows:")
        for row in element.metadata.get('row_data', []):
            print(f"  | {' | '.join(row)} |")

Content-Based Hashing

DOCX stamping hashes extracted text content rather than raw zip bytes. Style-only changes (font, color, spacing) do not affect the content hash. Structural changes (reordering paragraphs, adding headings) do.

CLI Usage

# Stamp a DOCX file
primestamp stamp contract.docx --preset standard

# Extract and display structure
primestamp fingerprint contract.docx --show-structure

# Verify a stamped DOCX
primestamp verify contract.docx

# Compare two versions
primestamp fingerprint --compare contract_v1.docx contract_v2.docx

Stamping Pipeline

When PrimeStamp stamps a DOCX file, it:

  1. Unzips the DOCX and parses the XML structure
  2. Extracts paragraphs, headings, tables, and other elements
  3. Reads metadata from core properties
  4. Captures comments, revisions, footnotes, and endnotes
  5. Computes a content hash from all extracted text
  6. Builds a Merkle tree over section hashes
  7. Optionally generates a Prime-Space fingerprint
  8. Creates a signed stamp with structural metadata
  9. Submits the hash to configured blockchain anchors

Comparison with PDF

Feature PDF DOCX
Extraction granularity Page-level Paragraph-level
Heading detection Heuristic (font-based) Explicit (Word styles)
Comments/revisions Annotations only Full revision tracking
Merkle tree Per page Per structural element
Dependencies pypdf python-docx