docx4j Word documents
v0.2.0Read, 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.
| Parameter | Type | Description |
|---|---|---|
template_path * | string | Path 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.
| Parameter | Type | Description |
|---|---|---|
data * | string | object | For 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 * | string | Where to write the filled .docx |
template_path * | string | Path to the .docx template |
headers_footers | boolean | Mail-merge mode: also merge fields in headers and footers (default true) |
overwrite | boolean | Replace output_path if it exists (default false) |
remove_sdts | boolean | XML mode: strip the content-control wrappers from the result (default true) |
remove_xml | boolean | XML 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.
| Parameter | Type | Description |
|---|---|---|
input_path * | string | Path to the .docx |
output_path * | string | Where to write the PDF |
overwrite | boolean | Replace output_path if it exists (default false) |
update_toc | boolean | Regenerate 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_xslt | boolean | Use 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.
| Parameter | Type | Description |
|---|---|---|
output_path * | string | Where to write the .docx |
input_path | string | Alternatively, path to a .md file |
markdown | string | The Markdown text (CommonMark + GFM tables, task lists, footnotes; $..$ TeX math) |
overwrite | boolean | Replace output_path if it exists (default false) |
styles_template_path | string | Optional .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.
| Parameter | Type | Description |
|---|---|---|
input_path * | string | Path to the .docx |
image_dir_path | string | Optional directory to extract images into (they are referenced from the Markdown); images are dropped otherwise |
output_path | string | Optional: write the Markdown to this file (required if the result exceeds the inline limit) |
overwrite | boolean | Replace output_path if it exists (default false) |
tracked_changes | string | How 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.
| Parameter | Type | Description |
|---|---|---|
output_path * | string | Where to write the .docx |
base_path | string | Directory (inside an allowed root) against which relative image paths are resolved; defaults to input_path's directory. Remote (http/https) images are never fetched. |
html | string | The HTML (loose HTML is fine; CSS in style attributes and <style> is honoured) |
input_path | string | Alternatively, path to an .html file |
mode | string | convert (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). |
overwrite | boolean | Replace output_path if it exists (default false) |
styles_template_path | string | Optional .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.
| Parameter | Type | Description |
|---|---|---|
input_path * | string | Path to the .docx |
image_dir_path | string | Optional directory to write images into (referenced relatively from the HTML when output_path is given); images are omitted otherwise |
output_path | string | Optional: write the HTML to this file (required if the result exceeds the inline limit) |
overwrite | boolean | Replace output_path if it exists (default false) |
update_toc | boolean | Regenerate the document's table of contents entries before rendering (no page numbers in HTML; default false) |
use_xslt | boolean | Use 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.
| Parameter | Type | Description |
|---|---|---|
input_path * | string | Path to a .docx file, inside one of the server's allowed root directories |
output_path | string | Optional: write the text to this file (required if the result exceeds the inline limit) |
overwrite | boolean | Replace output_path if it exists (default false) |