DraftAgent CLI for DOCX Automation
DraftAgent includes a native command-line interface for inspecting Word documents, applying structured transformations, and converting DOCX files to DOCX, HTML, or PDF.
For installation and general setup, see the DraftAgent documentation. This page describes the command-line contract and the currently supported options.
Compatibility note: DraftAgent converts documents into its canonical document model. Unsupported source XML and package parts are not preserved. Validate output with your documents before relying on the CLI in production workflows.
Installation
Install the CLI with Homebrew:
brew install draftagent/tap/draftagent-cli
The installed binary is named draftagent:
draftagent inspect input.docx
draftagent transform input.docx \
--replace "Draft" "Final" \
--output /tmp/transformed.docxCommands
inspect
Reads a DOCX and prints package and parsed-model information. Use --elements to list document elements and their session-scoped IDs.
draftagent inspect input.docx --elements --detail full --json
The --detail option applies to element output:
outlinelists the element structure and IDs;textadds text content; andfullalso includes run-level information and selector-ready model paths.
Inspection can include paragraphs, tables, rows, cells, images, unsupported blocks, styles, and effective formatting information. The JSON output is suitable for scripts and for finding IDs needed by structural transform options.
convert
Converts a DOCX to canonical DOCX, HTML, or PDF output. The output extension selects the format:
.docx— canonical OOXML;.htmlor.htm— HTML;.pdf— PDF; and
draftagent convert input.docx --output /tmp/document.html
draftagent convert input.docx --output /tmp/document.pdfcreate
Starts with a blank document and optionally appends paragraphs. The command creates a valid DOCX when the output has a .docx extension.
draftagent create \
--paragraph "Title" \
--paragraph "Created from scratch" \
--output /tmp/new-document.docx
Repeat --paragraph to add multiple paragraphs. Use an empty string when you need an intentional blank paragraph.
transform
Applies transformations to an input DOCX and writes the result to the requested output path. Convenience flags can be combined and are applied in command order where supported.
draftagent transform input.docx \
--replace "Draft" "Final" \
--set-run-style "Final" \
--append-paragraph "Approved" \
--output /tmp/transformed.docx
The output extension selects the result format, so transformations can also produce HTML or PDF:
draftagent transform input.docx \
--replace "Draft" "Final" \
--output /tmp/transformed.htmlCommon options
All commands support --json for a machine-readable success summary. Human-readable status messages are the default. Warnings and diagnostics are written to stderr.
draftagent --json transform input.docx \
--replace Draft Final \
--output output.docx
Additional global and command options include:
--performance, which prints elapsed execution time to stderr;--strictoncreateandtransform, which treats transformation warnings as errors; and--output OUTPUT, which selects the destination path and, for conversion and transformation, the output format by extension.
The convert and create commands create missing parent directories. The CLI writes only the requested output path and does not leave preview PDFs or other additional artifacts.
Transform options
The CLI supports text replacement, character and paragraph formatting, lists, structural edits, and selected table operations.
Text and formatting
--replace SEARCH REPLACEMENTreplaces all matching occurrences and may be repeated;--append-paragraph TEXTappends a paragraph and may be repeated;--set-run-style TEXTapplies bold styling to matching runs;--italic TEXTand--underline TEXTapply character styling to matching runs;--font TEXT FONT,--font-size TEXT POINTS, and--color TEXT COLORapply character formatting to matching runs;--paragraph-alignment BLOCK_ID ALIGNMENTacceptsleft,center,right, orjustify;--paragraph-spacing BLOCK_ID BEFORE AFTER LINEsets spacing in twentieths of a point;--heading BLOCK_ID LEVELapplies a heading level from 1 through 9; and--replace-paragraph BLOCK_ID TEXTreplaces paragraph runs while preserving the paragraph ID.
For example:
draftagent transform input.docx \
--italic "Important" \
--font "Important" "Aptos" \
--font-size "Important" 14 \
--color "Important" "1F4E79" \
--paragraph-spacing el_42 120 240 276 \
--heading el_42 2 \
--output /tmp/formatted.docxLists
--bulleted-list BLOCK_IDand--numbered-list BLOCK_IDconvert paragraphs to lists and may be repeated;--list-level BLOCK_ID LEVELchanges list nesting, starting at level 0;--remove-list BLOCK_IDremoves list formatting;--insert-list-item ANCHOR_ID TEXTinserts a list item;--move-list-item ITEM_ID BEFORE_IDmoves a list item; and--delete-list-item ITEM_IDdeletes a list item.
Structural blocks
--insert-paragraph-before BLOCK_ID TEXTand--insert-paragraph-after BLOCK_ID TEXTinsert paragraphs;--delete-block BLOCK_IDdeletes a block;--move-block-before BLOCK_ID DESTINATION_IDand--move-block-after BLOCK_ID DESTINATION_IDmove blocks; and--replace-paragraph BLOCK_ID TEXTreplaces paragraph content while preserving its element ID.
Empty paragraphs are supported by paragraph creation and insertion options. Use an empty string for a deliberate blank paragraph.
Tables
The CLI currently supports selected table edits:
--add-table-row TABLE_IDand--delete-table-row TABLE_ID ROW_ID;--add-table-column TABLE_ID INDEXand--delete-table-column TABLE_ID INDEX;--set-cell-text CELL_ID TEXT;--merge-cells TABLE_ID ROW START_COLUMN END_COLUMN; and--split-cell CELL_ID.
draftagent transform input.docx \
--add-table-row el_100 \
--add-table-column el_100 1 \
--set-cell-text el_101 "Updated value" \
--output /tmp/table.docx
Table creation, table widths, borders, shading, and path-based selectors are not currently exposed as convenience flags. Use the MCP interface or library APIs for those capabilities.
Element IDs and selectors
Use inspect --elements to find IDs for structural options:
draftagent inspect input.docx --elements --detail outline
An ID such as el_42 is valid only for the loaded document model and its transformation lineage. IDs are not stable across a new invocation or a new document load. The current CLI options primarily accept element IDs rather than MCP-style path selectors. Paths are included in JSON inspection output for identifying elements and for use in operation manifests.
Ordered operation manifests
Use transform --operations PATH to apply a JSON operation manifest in array order. Each operation is resolved against the document state produced by the preceding operation, and the output is written only after the complete manifest succeeds.
A manifest may be a JSON array or an object containing an operations array:
{
"operations": [
{"op": "appendParagraph", "text": ""},
{
"op": "insertParagraph",
"position": "after",
"target": "sections[0].blocks[0]",
"paragraph": {"text": "Invoice totals"}
},
{
"op": "applyParagraphFormat",
"target": "sections[0].blocks[1]",
"style": {
"spacing": {"before": 120, "after": 240, "line": 276, "lineRule": "auto"}
}
}
]
}
Manifest targets may be element IDs or model paths. Paragraph operations accept plain text shorthand or a structured paragraph object with text or runs.
The currently supported manifest operation IDs are:
replaceTextappendParagraphinsertParagraphdeleteBlockreplaceParagraphContentsetHeadingLevelsetRunFormatapplyRunFormatapplyParagraphFormatsetCellTextaddRowaddColumndeleteColumnsplitCell
Unsupported operation IDs fail with the operation index and do not write an output file. The manifest format follows the same operation naming conventions as the MCP interface, but CLI manifest coverage is currently only a subset of the MCP catalog.
Output and warnings
With --json, successful transform output includes the output path, format, applied transforms, and warnings. Human-readable diagnostics are printed to stderr, and CLI errors are currently human-readable rather than MCP-style structured JSON errors.
PDF conversion can emit font-substitution and other renderer warnings on stderr when a requested font is unavailable. For example, Calibri may be replaced by Carlito. These warnings do not mean conversion failed. Use --strict where supported if warnings should fail the command.
CLI and MCP
The CLI and MCP server share the canonical document model and transformation implementations. They are separate interfaces with different coverage:
- the CLI provides shell-friendly convenience flags and a partially supported ordered manifest;
- MCP exposes a broader structured operation catalog and path-aware request schemas; and
- a capability available through MCP is not necessarily available as a CLI flag or manifest operation.
Use the CLI for local scripts and automation, and use MCP when an agent needs runtime tool discovery, structured selectors, or the broader transformation catalog.