Automation

Automate a DOCX with Python

Accept tracked changes and save a separate DOCX from Python.

Use the Python SDK when application code already knows which document operation to run. This guide accepts every tracked change in a DOCX and writes the result to a separate file.

1. Install the Python SDK

SuperDoc supports Python 3.9 and newer:

python -m pip install superdoc-sdk

The package installs the matching SuperDoc CLI companion automatically. You do not need Node.js or a separate CLI installation. Published companions support macOS on Apple Silicon and Intel, Linux on ARM64 and x64, and Windows on x64.

2. Prepare a document

Download the tracked-changes fixture and save it as contract.docx in your working directory. It contains one tracked change.

Download the tracked-changes fixtureSynthetic NDA with one tracked changeDOCX

3. Accept the changes and save

Create accept_changes.py:

from superdoc import SuperDocClient


def is_successful_receipt(value: object) -> bool:
    return isinstance(value, dict) and value.get("success") is True


with SuperDocClient() as client:
    document = client.open({"doc": "./contract.docx"})

    try:
        receipt = document.track_changes.decide(
            {
                "decision": "accept",
                "target": {"kind": "all"},
            }
        )
        if not is_successful_receipt(receipt):
            raise RuntimeError("Accepting tracked changes failed.")

        document.save(
            {
                "out": "./contract.accepted.docx",
                "force": True,
            }
        )
    finally:
        document.close({"discard": True})

Run it from the directory containing contract.docx:

python accept_changes.py

SuperDocClient owns one persistent host process. The context manager starts it on entry and disposes it on exit. The document still has its own lifecycle, so the finally block closes the session even when a mutation or save fails.

4. Verify the output

Open contract.accepted.docx in Microsoft Word or the SuperDoc editor. Every tracked change should be accepted. The original contract.docx should remain unchanged.

Use the asynchronous client

Use AsyncSuperDocClient when the surrounding application already runs an event loop. Its document methods mirror the synchronous client and are awaited:

import asyncio

from superdoc import AsyncSuperDocClient


async def main():
    async with AsyncSuperDocClient() as client:
        document = await client.open({"doc": "./contract.docx"})
        try:
            info = await document.info({})
            print(info["counts"])
        finally:
            await document.close({"discard": True})


asyncio.run(main())

Use the Node.js SDK for the same deterministic workflow from JavaScript or TypeScript. For the operation and receipt model shared by both SDKs, continue with the Document API mental model.

On this page