Skip to main content

docx-editor

Release Build status codecov Commit activity License

Pure Python library for Word document track changes and comments, without requiring Microsoft Word.

Note: The PyPI package is named docx-editor because docx-edit was too similar to an existing package.

Features

  • Hash-Anchored Paragraph References: list_paragraphs() returns stable, hash-based paragraph IDs for safe, unambiguous targeting
  • Paragraph Location: get_paragraph_location(ref) reports whether a paragraph lives in the body or inside a table cell — with w:gridSpan-aware logical column, row, table index, and nesting depth. list_paragraph_locations() returns (ref, location) for every paragraph in one batch pass, avoiding a per-paragraph table rescan
  • Batch Editing: Atomic batch_edit() with upfront hash validation across all operations
  • Paragraph Rewrite: rewrite_paragraph() with automatic word-level diffing — specify desired text, get fine-grained tracked changes
  • Track Changes: Replace, delete, and insert text with revision tracking
  • Cross-Boundary Editing: Find and replace text spanning multiple XML elements and revision boundaries
  • Mixed-State Editing: Atomic decomposition for text spanning <w:ins>/<w:del> boundaries
  • Comments: Add, reply, resolve, and delete comments
  • Word's Tracking Switch: Saving a redline turns Track Changes on in settings.xml, so the recipient's own edits stay tracked too; documents protected with Word's Restrict Editing are refused at open rather than silently edited
  • Rationale Notes: note= on any text edit (replace/delete/insert_*/rewrite_paragraph, and per EditOperation inside batch_edit) anchors a comment explaining the redline on exactly the revisions it creates — and deletes it when they are accepted or rejected
  • Revision Management: List, accept, and reject tracked changes at three granularities — individual revisions, groups (one logical edit), and changesets (one whole batch_edit/batch_rewrite call); EditResult and Revision objects carry group_id and changeset_id
  • Session Mode: Optional persistent kernel (docx-session start/exec/eval/status/stop) keeps documents open across many small commands — ideal for AI agents (pip install "docx-editor[session]")
  • Cross-Platform: Works on Linux, macOS, and Windows
  • No Dependencies: Only requires defusedxml for secure XML parsing

Scope

This library redlines and reviews existing Word documents; it deliberately does not try to be a Word automation suite. Redaction is a permanent refusal — a tool built to preserve history cannot honestly promise removal, and a redaction that leaves the text recoverable in the file is worse than none. Also out of scope: creating documents and editing structure (tables, images, styles, sections, TOCs, fields, content controls, mail merge — use python-docx, and pandoc for format conversion); regex find/replace, which matches over text this library deliberately keeps anchored to runs and revisions; and diffing two arbitrary documents, whose domain-correct version is list_revisions() plus get_visible_text()/get_original_text() and rewrite_paragraph()'s own word-level diff. Schema validators are not on the roadmap either: the contract that matters is "opens in Word with zero repair prompts", which a round-trip corpus tests honestly and a validator does not. Text inside shapes and text boxes is excluded rather than half-editable, and container parts (headers, footers, footnotes, endnotes) wait for a real demand.

Installation

pip install docx-editor             # editing: track changes, comments, revisions
pip install "docx-editor[create]"   # + python-docx, for creating new documents
pip install "docx-editor[session]"  # + docx-session persistent CLI

Claude Code Plugin

This repo includes a plugin for Claude Code that enables AI-assisted Word document editing.

This plugin extends the original Anthropic docx skill which requires Claude to manually manipulate OOXML. Instead, this plugin provides an interface (docx-editor) that handles all the complexity—Claude just calls simple Python methods like doc.replace() or doc.add_comment(), making document editing significantly faster and less error-prone.

Install as plugin

# Add the marketplace
/plugin marketplace add pablospe/docx-editor

# Install the plugin
/plugin install docx-editor@docx-editor-marketplace

# Install dependencies
pip install "docx-editor[create]"

Manual install (alternative)

# Install dependencies
pip install "docx-editor[create]"

# Copy skill to Claude Code skills directory
git clone https://github.com/pablospe/docx-editor /tmp/docx-editor
mkdir -p ~/.claude/skills
cp -r /tmp/docx-editor/skills/docx ~/.claude/skills/
rm -rf /tmp/docx-editor

Once installed, Claude Code can help you edit Word documents with track changes, comments, and revisions.

Quick Start

from docx_editor import Document
import os

author = os.environ.get("USER") or "Reviewer"
with Document.open("contract.docx", author=author) as doc:
    # Step 1: List paragraphs with hash-anchored references
    for p in doc.list_paragraphs():
        print(p)
    # Output: P1#a7b2| Introduction to the contract
    #         P2#f3c1| The committee shall review
    # A paragraph over 80 chars is cut and its preview ends in "..." (not document text) —
    # quote search text from list_paragraphs_structured(limit=None) instead.

    # Step 2: Edit — each method returns the new paragraph ref
    r = doc.replace("30 days", "60 days", paragraph="P2#f3c1")
    doc.replace("net", "gross", paragraph=r)  # chain without list_paragraphs()
    doc.delete("obsolete text", paragraph="P5#d4e5")
    doc.insert_after("Section 5", " (as amended)", paragraph="P3#b2c4")
    doc.insert_before("Section 6", "See also ", paragraph="P6#e7f8")
    # Repeated text? Pass occurrence= (0-based) to pick which match; omitting it
    # requires a unique match, else AmbiguousTextError.
    doc.replace("the", "The", paragraph="P4#c5d6", occurrence=2)

    # Rewrite entire paragraph (automatic word-level diff)
    doc.rewrite_paragraph("P2#f3c1",
        "The board shall approve the updated proposal.")

    # Comments
    doc.add_comment("Section 5", "Please review")

    # Revision management
    revisions = doc.list_revisions()
    doc.accept_revision(revision_id=1)

    doc.save()

Cross-Boundary Text Operations

Text in Word documents with tracked changes can span revision boundaries. docx-editor handles this transparently:

from docx_editor import Document
import os

author = os.environ.get("USER") or "Reviewer"
with Document.open("reviewed.docx", author="Editor") as doc:
    # Get visible text (inserted text included, deleted excluded)
    text = doc.get_visible_text()

    # List paragraphs to find hash-anchored references. Large documents:
    # a bare call returns at most 200 paragraphs, ending with a
    # "... N more paragraphs; use start=201 or limit=None" notice.
    # Refs stay globally indexed across pages (page 2 starts at P201, not P1).
    page1 = doc.list_paragraphs()                 # up to P200, then the notice
    page2 = doc.list_paragraphs(start=201)        # next page, per the notice
    everything = doc.list_paragraphs(limit=None)  # uncapped, never a notice

    # Find text across element boundaries
    match = doc.find_text("Aim: To")
    if match and match.spans_revision:
        print("Text spans a revision boundary")

    # Replace works even across revision boundaries. Instead of a ref, you can
    # pass the match itself — it carries the text, paragraph and occurrence:
    #     doc.replace(match, "Goal: To")
    doc.replace("Aim: To", "Goal: To", paragraph="P1#a7b2")

    doc.save()

Batch Editing

Apply multiple edits atomically with upfront hash validation:

from docx_editor import Document, EditOperation

with Document.open("contract.docx", author="Editor") as doc:
    refs = doc.list_paragraphs()
    doc.batch_edit([
        EditOperation.replace("old", "new", paragraph="P2#f3c1"),
        EditOperation.delete("remove this", paragraph="P5#d4e5"),
    ])
    doc.save()

Pass dry_run=True to validate every operation up front without touching the document — doc.batch_edit(ops, dry_run=True) returns a list of EditValidationResult instead of applying the edits.

Saving into synced folders

docx-editor is safe to use inside cloud-synced folders (OneDrive, Dropbox, Google Drive, iCloud) and while Word is running.

  • Atomic save. save() writes the new document to a temporary file in the destination's own directory and promotes it with a single atomic rename, flushed to disk. The destination is never observed half-written, so a sync client can never upload a torn file. If the write (or validate=True) fails, the original is left exactly as it was — a failed validation can no longer destroy your document. The saved file keeps the original's permissions, and a symlinked destination is followed to the file it points at.

    Because the temp file is created next to the destination, saving needs write permission on the containing directory, not just on the document itself. If the directory is read-only, save() raises PermissionError.

    A write-protected document is refused, not silently replaced: save() raises PermissionError if the destination is read-only, even though the rename itself would have been permitted by the directory.

    An atomic rename replaces the file's inode, so state bound to the old inode does not survive it: the saved document keeps its permissions, but its ownership, POSIX ACLs, extended attributes, and any hardlinks to it do not carry over. This is inherent to atomic saving (every editor that writes this way behaves the same). If a document depends on an ACL or a hardlink, save to a new path.

  • Open-in-Word guard. Before writing, save() checks for the ~$ owner (lock) file Word places next to any open document. If the destination looks open, it raises DocumentOpenError rather than racing Word's writes:

    from docx_editor import Document, DocumentOpenError
    
    try:
        doc.save()
    except DocumentOpenError:
        # Someone has this document open in Word — close it and retry.
        ...
    

    If you are certain the ~$ file is a stale lock left by a crashed session, pass force=True to save anyway: doc.save(force=True).

  • Limitation — remote co-authoring is undetectable. The guard only sees a local ~$ file. A document being edited remotely (OneDrive/SharePoint co-authoring, or Word for the web) leaves no local lock file, so it cannot be detected from the filesystem. In that case, rely on the cloud provider's version history to recover if edits collide.

Metadata

Release files for docx-editor 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for docx-editor 0.9.0
File Size Uploaded Provenance Actions
docx_editor-0.9.0.tar.gz 1.0 MB Without provenance Details for docx_editor-0.9.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for docx-editor 0.9.0
File Interpreter ABI Platform Actions
docx_editor-0.9.0-py3-none-any.whl Python 3 none any Details for docx_editor-0.9.0-py3-none-any.whl

Total release size: 1.3 MB

Release files / docx_editor-0.9.0.tar.gz

Download URL docx_editor-0.9.0.tar.gz
Size 1.0 MB
Tags Source
SHA-256 checksum
How to use checksums
b6878b160baf6fec34e098aed99ee2773bdff8dc5a083b16ced5c01af5816177
BLAKE2b-256 checksum
How to use checksums
240e93eb0f25590b956d2c43f88642580ca400f6bea7548fb2d1e45a634d1e8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / docx_editor-0.9.0-py3-none-any.whl

Download URL docx_editor-0.9.0-py3-none-any.whl
Size 220.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7259a70e4b859f81451c86e5283b9f54c5b2af82e545b4f5970c886f66c637aa
BLAKE2b-256 checksum
How to use checksums
5bb7e707d0f1187dbe4f803c498b99937b07f53f46a936d06c633c2610d2b715
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for docx-editor 0.9.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page