
docx-rs
Open Source Rust & WebAssembly Library for Word Documents
Generate, Parse & Manipulate .docx Files via Rust API and JavaScript Bindings.
What is docx-rs?
docx-rs is an open source Rust library that enables developers to read, write, and manipulate .docx files programmatically. Built with performance and safety in mind, it supports full DOCX generation and parsing using a clean, idiomatic Rust API. The library also exposes WebAssembly bindings for browser and Node.js environments, allowing seamless integration into web applications. With docx-rs, you can create complex Word documents—including tables, images, hyperlinks, and styles—without relying on external dependencies or Microsoft Office. Its modular design and strong typing ensure reliability and maintainability across large-scale projects.
docx-rs follows the Office Open XML (OOXML) standard and supports key features such as paragraphs, runs, text formatting, tables, headers, footers, and embedded media. It also provides robust parsing capabilities, converting existing .docx files into a structured Rust model that can be serialized to JSON. The library is actively maintained, well-documented, and tested with comprehensive examples. Whether you're building server-side document automation, client-side reporting tools, or cross-platform office suites, docx-rs delivers a lightweight, dependency-free solution for working with Word documents in Rust and JavaScript.
Getting Started with docx-rs
To start using docx-rs, add it to your Rust project via Cargo. The library is available on crates.io and docs.rs. For JavaScript/TypeScript projects, install the docx-wasm package via npm or pnpm. The library requires no external binaries and works offline. You can generate documents with just a few lines of code using the fluent API. Check the GitHub repository for examples, documentation, and contribution guidelines. It supports Rust 1.88+ and is fully tested with snapshot-based testing.
Install docx-rs via Cargo and npm
[dependencies]
docx-rs = "0.4"pnpm add docx-wasm file-saverGenerate Word Documents with Rust
docx-rs provides a clean, type-safe API for building Word documents from scratch. You can add paragraphs, runs, text formatting, tables, and more using a fluent builder pattern. The library handles XML generation and ZIP packaging automatically via the Docx::pack method. It supports inline and floating images, headers, footers, page settings, and custom properties. With strong typing and zero unsafe code, docx-rs ensures reliability and security. You can also serialize the document model to JSON for debugging or further processing. This makes it ideal for server-side document generation, reporting tools, and automated office workflows.
How to Create a Simple Word Document in Rust?
use docx_rs::*;
fn main() -> Result<(), Box> {
let file = std::fs::File::create("./hello.docx")?;
Docx::new()
.add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello")))
.pack(file)?;
Ok(())
} Parse & Export DOCX to JSON
docx-rs includes a powerful parser that reads existing .docx files into a structured Rust model. This model supports full round-trip processing and can be serialized to JSON for interoperability or analysis. The parser preserves important metadata such as footnote references with w:id and optional w:customMarkFollows flags. You can disable image preview generation to avoid decoding embedded media, retrieving raw bytes instead. This is useful for applications that need to inspect or transform document content without rendering. The JSON output is clean, predictable, and compatible with downstream tools and services.
How to Parse a DOCX File and Export to JSON in Rust?
use docx_rs::*;
fn main() -> Result<(), Box> {
let bytes = std::fs::read("./hello.docx")?;
let docx = read_docx(&bytes)?;
println!("{}", docx.json());
Ok(())
} Use docx-rs in the Browser
docx-rs exposes WebAssembly bindings via the docx-wasm package, enabling client-side document generation in modern browsers. You can create, edit, and download .docx files entirely on the client without server round-trips. The API mirrors the Rust interface, supporting paragraphs, runs, text, and more. Integration with libraries like file-saver allows seamless file downloads. This is perfect for web-based reporting tools, form-to-document converters, and offline-capable productivity apps. Since it's compiled to WebAssembly, performance is near-native and memory-safe.
How to Generate a DOCX in the Browser with JavaScript?
import { saveAs } from "file-saver";
import("docx-wasm").then((w) => {
const buffer = new w.Docx()
.addParagraph(new w.Paragraph().addRun(new w.Run().addText("Hello world!")))
.build();
saveAs(new Blob([buffer]), "hello.docx");
});Read & Write DOCX in Node.js
docx-rs works seamlessly in Node.js environments via the docx-wasm package. You can generate and parse .docx files on the server using familiar JavaScript patterns. The API supports asynchronous loading of the WebAssembly module and returns buffers compatible with Node's fs module. This makes it ideal for backend services that need to process Word documents—such as document conversion APIs, template engines, or automated reporting systems. Since it avoids external dependencies like LibreOffice or Word, deployment is simpler and more secure.
How to Write a DOCX File in Node.js?
const w = require("docx-wasm");
const { writeFileSync } = require("fs");
const buffer = new w.Docx()
.addParagraph(new w.Paragraph().addRun(new w.Run().addText("Hello world!")))
.build();
writeFileSync("hello.docx", Buffer.from(buffer));