[Guides](/safe-docx/guides.md) / Convert DOCX to HTML

Beginner ⏱ 10 min

# Convert DOCX to HTML with an AI agent

Render a Word .docx to semantic HTML with safe-docx and an AI agent — paragraphs, headings, lists, tables, and images, ready for previews and the web.

Turning a Word document into HTML usually means a tradeoff: `mammoth.js` gives you clean semantic markup but needs a Node project, `docx-preview` aims for high-fidelity rendering in the browser, and a headless LibreOffice conversion is accurate but a heavy dependency to run. If you already use an AI coding agent, safe-docx's `export` tool emits semantic HTML in-session, with no extra runtime and nothing leaving your machine.

## How DOCX-to-HTML options compare

| Capability | safe-docx `export` | mammoth.js | LibreOffice (headless) |
| --- | --- | --- | --- |
| Runs inside your AI agent (MCP) | Yes | No (JS library) | No (binary) |
| Output style | Semantic HTML | Semantic HTML | High-fidelity HTML+CSS |
| Tables with colspan/rowspan | Yes | Limited | Yes |
| Extracts embedded images | Yes | Yes | Yes |
| Pixel-faithful layout | No (by design) | No | Yes |
| Extra runtime to install | None (npx) | Node project | LibreOffice install |

safe-docx emits the semantic tier — structural, not pixel-exact. A pixel-faithful PDF/print path is a separate, heavier problem and is not part of export today; to produce a PDF from the exported HTML, see [Convert DOCX to PDF](/safe-docx/guides/convert-docx-to-pdf.md).

## The workflow, step by step

1.  1
    
    ### Install safe-docx for your agent
    
    Add the Apache-2.0-licensed safe-docx MCP server to your agent once. For Claude Code:
    
    ```
    claude mcp add safe-docx -- npx -y @usejunior/safe-docx
    ```
    
    The same server works with Gemini CLI, Cursor, and Codex.
    
2.  2
    
    ### Get a .docx to convert
    
    Use your own document or download a real contract template to follow along, for example `bonterms-mutual-nda.docx`, saved next to your project.
    
3.  3
    
    ### Ask the agent to export it to HTML
    
    Ask in plain language: _“Export bonterms-mutual-nda.docx to HTML.”_ The agent calls the safe-docx `export` tool with the HTML format:
    
    ```
    export(file_path="bonterms-mutual-nda.docx", format="html")
    ```
    
    The tool writes `bonterms-mutual-nda.html` next to the source and returns its path, byte count, and the rendered HTML.
    
4.  4
    
    ### Review the semantic HTML output
    
    safe-docx emits the _semantic_ tier of HTML: structural elements that mirror the document's outline rather than a pixel-faithful clone. Paragraphs become `<p>`, headings become `<h1>`–`<h6>`, lists become nested `<ul>`/`<ol>`, and tables become `<table>` with `colspan`/`rowspan` derived from merged cells. A converted clause looks like this:
    
    ```
    <h2>1. Confidential Information</h2>
    <p><strong>Confidential Information</strong> means any non-public
    information disclosed by one party to the other.</p>
    ```
    
5.  5
    
    ### Handle images, footnotes, and the layout it skips
    
    Embedded images are extracted from the document's media and emitted as `<img>`; footnotes and comments become anchors and `<aside>` elements. Because this is the semantic tier, it deliberately skips equations, text boxes, charts, and fixed page geometry — use it for previews, web rendering, and content extraction, not as a print-exact reproduction.
    

## Related guides

### [Convert DOCX to Markdown](/safe-docx/guides/convert-docx-to-markdown.md)

### [Convert DOCX to PDF](/safe-docx/guides/convert-docx-to-pdf.md)

## Frequently asked questions

<details>
<summary>Can safe-docx convert a DOCX file to HTML?</summary>

Yes. The export tool renders an open .docx to semantic HTML when you pass format html. It emits paragraphs, headings, nested lists, and tables with colspan and rowspan, and extracts embedded images.
</details>

<details>
<summary>Is the HTML pixel-perfect like the Word document?</summary>

No. safe-docx produces the semantic tier — structural HTML that mirrors the document outline, not a pixel-faithful clone. It is meant for previews, web rendering, and content extraction. Equations, text boxes, charts, and fixed page layout are not represented.
</details>

<details>
<summary>What happens to images and footnotes?</summary>

Images are extracted from the document media and emitted as img elements. Footnotes and comments become anchors and aside elements so the references survive the conversion.
</details>

<details>
<summary>Which agents can run the conversion?</summary>

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.
</details>

## Convert and edit DOCX from your agent

Install safe-docx, point your agent at a Word file, and export it to HTML — or edit it in place.

[About safe-docx](/products/safe-docx.md) [Browse templates](https://openagreements.org/templates)
