Skip to main content
Safe Docx lets you read, search, and precisely modify existing Word documents without touching their formatting. This guide walks through the core editing workflow.
All edits operate on an in-memory session. Nothing is written to disk until you call save.

Core editing workflow

1

Read the file and discover paragraph IDs

Call read_file with the path to your .docx file. Safe Docx returns the document content annotated with _bk_* paragraph IDs.
Each paragraph in the output is tagged with a stable identifier like _bk_42 or _bk_p3c. These IDs are your handles for all subsequent edit operations. You must reference them exactly — including the _bk_ prefix.
Paragraph IDs are document-specific and not sequential across files. Always read the target file first to obtain the correct IDs. Do not guess or reuse IDs from a different document.
2

Choose a read format

read_file supports three output formats via the format parameter:For agent workflows, use toon to minimize token usage:
By default, read_file is token-limited to approximately 14k tokens and returns pagination metadata (has_more, next_offset). Use offset and limit to paginate through large documents.
3

Search for specific text

Use grep to locate paragraphs containing specific terms without reading the entire document:
grep returns matching paragraph IDs and surrounding context. You can also search multiple files at once using the file_paths array for stateless multi-file search.
4

Replace text in a paragraph

Call replace_text with the paragraph ID, the exact text to replace, and the replacement:
If the target text is fragmented across formatting runs, set normalize_first: true to merge format-identical adjacent runs before searching.
old_string must match the paragraph text exactly, including whitespace. If the match fails, check that the paragraph ID is correct and that you are not including [^N] footnote markers, which are display-only.
5

Insert a new paragraph

Use insert_paragraph to add content before or after an existing paragraph:
Set position to "BEFORE" or "AFTER" relative to the anchor paragraph. Use style_source_id to clone paragraph formatting from a different paragraph instead of the anchor.
6

Save the output

Call save to write the result to disk. You can save a clean version, a tracked-changes version, or both:

Real agent prompt example

This prompt reliably produces a clean and tracked-changes output:
Always specify absolute paths in your prompts. Relative paths are more likely to be misinterpreted by the agent or rejected by the path policy.

Comparing documents

Produce tracked-changes output from two DOCX versions.

Batch editing

Plan and apply batches of edits with multi-agent coordination.