Convert DOCX to Markdown with an AI agent
Turn an existing Word .docx into clean Markdown with safe-docx and an AI agent — headings, lists, links, tables, and footnotes preserved. Step-by-step.
Getting clean Markdown out of a Word document usually means reaching for pandoc,
scripting python-docx paragraph by paragraph, or unzipping the OOXML and parsing
document.xml by hand. Those work, but they live outside your agent and need their own
runtime. If you already drive an AI coding agent, safe-docx exposes a single export tool
that does the conversion in-session — no extra toolchain, and document content never leaves your machine.
How DOCX-to-Markdown options compare
| Capability | safe-docx export |
pandoc | mammoth.js |
|---|---|---|---|
| Runs inside your AI agent (MCP) | Yes | No (external CLI) | No (JS library) |
| Headings, lists, links, bold/italic | Yes | Yes | Yes |
| GitHub-flavored tables | Yes (lossy on merged cells) | Yes | Limited |
| Footnotes | Yes ([^n]) | Yes | No |
| Extra runtime to install | None (npx) | pandoc binary | Node project |
| Also edits the .docx in place | Yes (separate tools) | No | No |
safe-docx export is one-way and lossy by design. For a feature-by-feature breakdown of the editing surface, see safe-docx vs python-docx.
The workflow, step by step
-
1
Install safe-docx for your agent
safe-docx is an MIT-licensed MCP server that runs locally via
npx. Add it to your agent once. For Claude Code:claude mcp add safe-docx -- npx -y @usejunior/safe-docxIt works the same way with Gemini CLI, Cursor, and Codex — any MCP-compatible client.
-
2
Get a .docx to convert
Use a document you already have, or download a real contract template to follow along. Save it next to your project, for example
bonterms-mutual-nda.docx. -
3
Ask the agent to export it to Markdown
Tell your agent in plain language: “Export bonterms-mutual-nda.docx to Markdown.” Under the hood the agent calls the safe-docx
exporttool with the Markdown format:export(file_path="bonterms-mutual-nda.docx", format="markdown")Markdown is the default format, so
export(file_path="bonterms-mutual-nda.docx")does the same thing. The tool writesbonterms-mutual-nda.mdnext to the source and returns its path, byte count, and the rendered Markdown. -
4
Review the Markdown output
The emitter walks the document's structure: headings become
#..######, bold and italic runs become**bold**and*italic*, hyperlinks become Markdown links, numbered and bulleted lists keep their nesting, tables become GitHub-flavored Markdown tables, and footnotes become[^n]references. A converted heading and clause look like this:## 1. Confidential Information "Confidential Information" means any **non-public** information disclosed by one party to the other, whether disclosed [in writing](https://example.com) or orally. -
5
Know what export does and doesn't do
Export is intentionally lossy and one-way: it produces a clean textual rendering, not a round-trippable copy. Merged table cells flatten, and equations, text boxes, charts, and absolute page layout are not represented. There is no Markdown-to-DOCX reverse step — to change the Word file itself, use safe-docx's in-place editing tools (
replace_text,insert_paragraph) instead.
Frequently asked questions
Can safe-docx convert a DOCX file to Markdown?
Yes. The export tool renders an open .docx to Markdown when you pass format markdown (the default). It preserves headings, bold and italic, links, lists, GitHub-flavored tables, and footnotes.
Is the DOCX-to-Markdown conversion lossy?
Yes, intentionally. Export is a one-way, semantic rendering — not a round-trip. Merged table cells flatten, and equations, text boxes, charts, and exact page layout are not represented.
Can I convert Markdown back into the original DOCX?
No. There is no reverse step. safe-docx edits existing .docx files in place with tools like replace_text and insert_paragraph, so you change the Word document directly rather than rebuilding it from Markdown.
Which agents can run the conversion?
Any MCP-compatible client — Claude Code, Gemini CLI, Cursor, and Codex — once safe-docx is installed. It runs locally via npx, so document content stays on your machine.
Convert and edit DOCX from your agent
Install safe-docx, point your agent at a Word file, and export it to Markdown — or edit it in place.