Crate docx_rust

Source
Expand description

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

Modules§

app
Application-Defined File Properties part
content_type
Content-type item
core
Core File Properties part
document
font_table
Font Table part
formatting
Formatting
media
rels
Relationship item
settings
Settings part
styles
Style Definitions
web_settings
WebSettings part

Structs§

Docx
A WordprocessingML package
DocxFile
An extracted docx file

Enums§

DocxError
Error type of docx-rs

Functions§

write_attr

Type Aliases§

DocxResult
Specialized Result which the error value is DocxError.