docx4j Word documents

v0.2.0
io.github.plutext/docx4j-mcp Checked 1 day ago Published 2 months ago

Read, convert and fill Word (.docx) documents: templates, Markdown/HTML in and out, PDF with math.

These tools were reported by the server itself during our smoke test (tools/list), not copied from the README.

describe_template Describe a docx template

Inspects a Word template and reports what data it wants: for an OpenDoPE or content-control template, the skeleton XML to fill in (with the bound xpaths, repeats and conditions); for a mail-merge template, the MERGEFIELD names. Call this before fill_template.

ParameterTypeDescription
template_path *stringPath to the .docx template (inside an allowed root)

fill_template Fill a docx template

Fills a Word template deterministically, preserving all its formatting. OpenDoPE and bound content-control templates take 'data' as an XML string (repeats and conditions are processed); mail-merge templates take 'data' as a JSON object of MERGEFIELD name to value. Use describe_template first to learn which, and what the data should look like.

ParameterTypeDescription
data *string | objectFor OpenDoPE / content-control templates: the filled-in XML document as a string (start from describe_template's skeleton_xml). For mail-merge templates: a JSON object mapping MERGEFIELD names to valu…
output_path *stringWhere to write the filled .docx
template_path *stringPath to the .docx template
headers_footersbooleanMail-merge mode: also merge fields in headers and footers (default true)
overwritebooleanReplace output_path if it exists (default false)
remove_sdtsbooleanXML mode: strip the content-control wrappers from the result (default true)
remove_xmlbooleanXML mode: drop the custom XML data part from the result (default true)

convert_to_pdf Convert docx to PDF

Renders a Word document to PDF with docx4j's XSL-FO exporter (Apache FOP). Equations (OMML) are rendered (a very long single display equation will not line-wrap). Reports which fonts were substituted, so you can tell the user if the output may differ from Word.

ParameterTypeDescription
input_path *stringPath to the .docx
output_path *stringWhere to write the PDF
overwritebooleanReplace output_path if it exists (default false)
update_tocbooleanRegenerate the document's table of contents (entries and page numbers) before rendering, so it matches the output (default false; no effect if the document has no ToC)
use_xsltbooleanUse the older XSLT-based exporter instead of the default visitor exporter (default false)

markdown_to_docx Convert Markdown to docx

Creates a properly styled Word document from Markdown: headings, lists and numbering, GFM tables, code, footnotes and task lists become native Word constructs (no HTML detour), and TeX math ($..$ / $$..$$) becomes real, editable Word equations (OMML) that also render in PDF/HTML export. Give a styles_template_path to inherit an organisation's styles. Remote images are not fetched.

ParameterTypeDescription
output_path *stringWhere to write the .docx
input_pathstringAlternatively, path to a .md file
markdownstringThe Markdown text (CommonMark + GFM tables, task lists, footnotes; $..$ TeX math)
overwritebooleanReplace output_path if it exists (default false)
styles_template_pathstringOptional .docx whose styles, numbering, page setup and headers/footers the output should use; its body content is replaced

docx_to_markdown Convert docx to Markdown

Reads a Word document as Markdown (CommonMark + GFM): headings, lists, tables, footnotes and links are preserved as Markdown structure, and Word equations (OMML) become $..$ TeX math. Prefer this over extract_text when structure matters. Large results are truncated inline unless output_path is given.

ParameterTypeDescription
input_path *stringPath to the .docx
image_dir_pathstringOptional directory to extract images into (they are referenced from the Markdown); images are dropped otherwise
output_pathstringOptional: write the Markdown to this file (required if the result exceeds the inline limit)
overwritebooleanReplace output_path if it exists (default false)
tracked_changesstringHow to render tracked changes: accept them (default) or show insertions/deletions as markup

html_to_docx Convert HTML to docx

Creates a Word document from HTML using docx4j-ImportXHTML: headings, paragraphs, inline formatting, lists, tables, links and CSS become native Word markup, and <math> (MathML) becomes real, editable Word equations. Give a styles_template_path to inherit an organisation's styles (class names matching style names map to those styles). Remote images are not fetched; use data: URIs or local files under an allowed root.

ParameterTypeDescription
output_path *stringWhere to write the .docx
base_pathstringDirectory (inside an allowed root) against which relative image paths are resolved; defaults to input_path's directory. Remote (http/https) images are never fetched.
htmlstringThe HTML (loose HTML is fine; CSS in style attributes and <style> is honoured)
input_pathstringAlternatively, path to an .html file
modestringconvert (default): translate to native Word markup now. altchunk: embed the HTML for Word to convert when the file is opened (highest fidelity in Word, but docx4j cannot render it to PDF).
overwritebooleanReplace output_path if it exists (default false)
styles_template_pathstringOptional .docx whose styles, numbering, page setup and headers/footers the output should use; its body content is replaced

convert_to_html Convert docx to HTML

Renders a Word document to a standalone HTML page (CSS from the document's styles, fonts mapped to web font stacks); Word equations become native, accessible MathML (no JavaScript). For a lossless-ish text view prefer docx_to_markdown; use this when the layout/formatting matters or the HTML will be shown in a browser.

ParameterTypeDescription
input_path *stringPath to the .docx
image_dir_pathstringOptional directory to write images into (referenced relatively from the HTML when output_path is given); images are omitted otherwise
output_pathstringOptional: write the HTML to this file (required if the result exceeds the inline limit)
overwritebooleanReplace output_path if it exists (default false)
update_tocbooleanRegenerate the document's table of contents entries before rendering (no page numbers in HTML; default false)
use_xsltbooleanUse the older XSLT-based exporter instead of the default visitor exporter (default false)

extract_text Extract text from a docx

Returns the plain text of a Word document's body, one line per paragraph or table, with no structure. For headings, tables and notes as Markdown use docx_to_markdown instead.

ParameterTypeDescription
input_path *stringPath to a .docx file, inside one of the server's allowed root directories
output_pathstringOptional: write the text to this file (required if the result exceeds the inline limit)
overwritebooleanReplace output_path if it exists (default false)