#generator #openxml #parser

docx-rust

A Rust library for parsing and generating docx files

9 releases

0.1.11 Jan 22, 2026
0.1.10 May 26, 2025
0.1.9 Dec 17, 2024
0.1.8 May 21, 2024
0.1.3 Jul 17, 2022

#552 in Parser implementations

Download history 28257/week @ 2026-01-22 28629/week @ 2026-01-29 59501/week @ 2026-02-05 59783/week @ 2026-02-12 82891/week @ 2026-02-19 93805/week @ 2026-02-26 107501/week @ 2026-03-05 102748/week @ 2026-03-12 62592/week @ 2026-03-19 49473/week @ 2026-03-26 70463/week @ 2026-04-02 73881/week @ 2026-04-09 65614/week @ 2026-04-16 59244/week @ 2026-04-23 50833/week @ 2026-04-30 18295/week @ 2026-05-07

206,018 downloads per month
Used in 9 crates (6 directly)

MIT license

1MB
11K SLoC

GitHub Workflow Status Crates.io Document

docx

A Rust library for parsing and generating docx files.

fork of https://github.com/PoiScript/docx-rs

Document

License

MIT


lib.rs:

A Rust library for parsing and generating docx files.

Create a new document

Use Docx::default to create a new empty Docx, then use Docx::write_file for saving it to a file.

use docx_rust::document::Paragraph;
use docx_rust::Docx;

let mut docx = Docx::default();

// create a new paragraph and insert it
let para = Paragraph::default().push_text("Lorem Ipsum");
docx.document.push(para);

docx.write_file("demo.docx").unwrap();

Also see: Docx::write.

Reading from files

Use DocxFile::from_file to extract content from docx files, then use DocxFile::parse to generate a Docx struct.

use docx_rust::document::Paragraph;
use docx_rust::DocxFile;

let docx = DocxFile::from_file("origin.docx").unwrap();
let mut docx = docx.parse().unwrap();

let para = Paragraph::default().push_text("Lorem Ipsum");
docx.document.push(para);

docx.write_file("origin_appended.docx").unwrap();

To reduce allocations, DocxFile::parse returns a Docx struct contains references to DocxFile itself. It means you have to make sure that DocxFile lives as long as its returned Docx:

use docx_rust::DocxFile;

let mut docx_option = None;
{
    let docx_file = DocxFile::from_file("foo.docx").unwrap();
    let mut docx = docx_file.parse().unwrap();
    docx_option = Some(docx);
    // `docx_file` gets dropped here and code fails to compile
}
docx_option.unwrap().write_file("foo.docx").unwrap();

Also see: DocxFile::from_reader.

Similar Projects

bokuweb/docx-rs: A .docx file writer with Rust/WebAssembly.

License

MIT

Dependencies

~4MB
~84K SLoC