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 在 解析器实现
566 每月下载量
用于 2 crates
1MB
10K SLoC
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