# 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 [#1-install-the-python-sdk]

SuperDoc supports Python 3.9 and newer:

```bash
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 [#2-prepare-a-document]

Download the [tracked-changes fixture](/fixtures/tracked-changes.docx) and save it as `contract.docx` in your working
directory. It contains one tracked change.

[Download the tracked-changes fixture](/fixtures/tracked-changes.docx): Synthetic NDA with one tracked change · DOCX


> **Keep the source file (warning)**
>
> Write the first result to a separate path so you can compare the output with the original document.


## 3. Accept the changes and save [#3-accept-the-changes-and-save]

Create `accept_changes.py`:

```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`:

```bash
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 [#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.

> **Verification target (success)**
>
> The output contains the same document content and formatting with no pending tracked changes. The source file remains
> unchanged.


## Use the asynchronous client [#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:

```python
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](/agents/automation/node-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](/document-api/mental-model).
