Read and write .docx (Word) files locally as markdown — structural conversion via mammoth (read) and the docx library (write). No OCR, no cloud. Pairs with read-pdf for end-to-end document workflows.

769 Downloads

7 stars

README

@zexigh/docx — round-trip Word ↔ markdown for LM Studio

Two LM Studio tools to read and write .docx files locally, no cloud, no vision model. Designed to slot into pipelines like read_docx_to_markdown → anonymize_text → write_markdown_to_docx.

What it does

  • Reading uses mammoth to walk the .docx XML directly — it's a structural conversion, not OCR. A 30-page document takes a fraction of a second.
  • Writing parses the markdown with marked, walks the AST, and emits a Word document via docx. The generated file opens cleanly in Word, Pages, and LibreOffice.
  • Sister plugin to @zexigh/read-pdf: PDFs need a vision model because they're images-on-paper; .docx is structured XML so we read it directly. The right tool for the right format.

Tools exposed

read_docx_to_markdown(path, preserve_styles?, include_metadata?)

Returns { markdown, warnings, source_chars, metadata }. Headings, bold, italic, lists, blockquotes, links, and basic tables are preserved.

write_markdown_to_docx(path, markdown, title?)

Single-call write. Returns { written: { path, bytes, paragraphs }, backup_path, warnings }. If the target file exists, the previous version is copied to <root>/.lmstudio-fs-backup/<rel>.<ts>.bak before being overwritten.

Configuration (LM Studio plugin settings)

FieldDefaultUse
allowedPaths[]Roots under which .docx can be read or written. ~ is expanded. Set at least one.
defaultFontFamilyCalibriBody font of generated docs.
defaultFontSizePt11Body size in points.
pageMarginnormalnormal ≈ 2.54cm, narrow ≈ 1.27cm, wide ≈ 5.08cm.
preserveListStylestrueKeep numbered vs bulleted from the markdown source.
maxFileSizeMb50Reject .docx larger than this on read.
verboseLoggingfalseLog each conversion.

Round-trip fidelity (i1)

A document written by write_markdown_to_docx and read back by read_docx_to_markdown produces markdown that's structurally identical: heading levels, list types, bold/italic, links and paragraph boundaries are preserved.

Known small losses, kept as-is for i1:

  • Inline `code` round-trips as plain text (mammoth doesn't detect Consolas runs as code).
  • > blockquote round-trips as a single *italic* indented paragraph. Reason: mammoth's markdown writer (≤1.12) has no handler for <blockquote>; rendering blockquotes via italic+indent at least keeps paragraph boundaries intact.
  • Code blocks (```) round-trip as one monospace paragraph per line rather than a fenced block.

Limitations (i1)

No images, no complex tables (cell merges, custom borders), no tracked changes, no Word comments, no custom paragraph styles, no equations, no footnotes. Each of these is targeted in a later iteration (see PROMPT.md §11).

License

MIT.