#docx #generator #parser #openxml

docx-rust

一个用于解析和生成 docx 文件的 Rust 库

6 个版本

0.1.8 2024年5月21日
0.1.7 2024年5月1日
0.1.6 2023年12月31日
0.1.5 2023年3月30日
0.1.3 2022年7月17日

#614解析器实现

Download history 186/week @ 2024-05-01 8/week @ 2024-05-08 144/week @ 2024-05-15 120/week @ 2024-05-22 74/week @ 2024-05-29 367/week @ 2024-06-05 304/week @ 2024-06-12 245/week @ 2024-06-19 212/week @ 2024-06-26 343/week @ 2024-07-03 248/week @ 2024-07-10 272/week @ 2024-07-17 207/week @ 2024-07-24 127/week @ 2024-07-31 117/week @ 2024-08-07 93/week @ 2024-08-14

566 每月下载量
用于 2 crates

MIT 许可证

1MB
10K SLoC

GitHub Workflow Status Crates.io Document

docx

一个用于解析和生成 docx 文件的 Rust 库。

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

文档

许可证

MIT


lib.rs:

一个用于解析和生成 docx 文件的 Rust 库。

创建新文档

使用 Docx::default 创建一个新的空 Docx,然后使用 Docx::write_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();

另请参阅: Docx::write.

从文件读取

使用 DocxFile::from_file 从 docx 文件中提取内容,然后使用 DocxFile::parse 生成一个 Docx 结构体。

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();

为了减少分配,DocxFile::parse 返回一个包含对 DocxFile 自身引用的 Docx 结构体。这意味着您必须确保 DocxFile 存活时间至少与返回的 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();

另请参阅: DocxFile::from_reader.

类似项目

bokuweb/docx-rs:使用 Rust/WebAssembly 的 .docx 文件写入器。

许可证

MIT

依赖关系

~4MB
~80K SLoC