| Crates.io | linch-docx-rs |
| lib.rs | linch-docx-rs |
| version | 0.0.1-alpha.3 |
| created_at | 2026-01-15 13:10:21.428214+00 |
| updated_at | 2026-01-15 14:27:50.681212+00 |
| description | A reliable DOCX reading and writing library for Rust with round-trip preservation |
| homepage | https://github.com/laofahai/linch-docx-rs |
| repository | https://github.com/laofahai/linch-docx-rs |
| max_upload_size | |
| id | 2045521 |
| size | 375,951 |
A reliable DOCX reading and writing library for Rust with round-trip preservation.
Add to your Cargo.toml:
[dependencies]
linch-docx-rs = "0.1"
use linch_docx_rs::Document;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let doc = Document::open("example.docx")?;
// Get all text
println!("{}", doc.text());
// Iterate paragraphs
for para in doc.paragraphs() {
println!("Style: {:?}", para.style());
println!("Text: {}", para.text());
// Access runs (text with formatting)
for run in para.runs() {
if run.bold() {
print!("[BOLD] ");
}
println!("{}", run.text());
}
}
Ok(())
}
use linch_docx_rs::{Document, Run};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = Document::new();
// Add a heading
let heading = doc.add_paragraph("My Document");
heading.set_style("Heading1");
// Add a paragraph with formatted text
let para = doc.add_empty_paragraph();
let mut bold_run = Run::new("Bold text");
bold_run.set_bold(true);
para.add_run(bold_run);
let mut italic_run = Run::new(" and italic text");
italic_run.set_italic(true);
para.add_run(italic_run);
// Save
doc.save("output.docx")?;
Ok(())
}
use linch_docx_rs::Document;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = Document::open("existing.docx")?;
// Add new content
doc.add_paragraph("This paragraph was added by Rust!");
// Save - unknown elements are preserved!
doc.save("modified.docx")?;
Ok(())
}
| Method | Description |
|---|---|
Document::new() |
Create a new empty document |
Document::open(path) |
Open a document from file |
Document::from_bytes(bytes) |
Open a document from bytes |
doc.save(path) |
Save document to file |
doc.to_bytes() |
Save document to bytes |
doc.paragraphs() |
Iterate over paragraphs |
doc.tables() |
Iterate over tables |
doc.add_paragraph(text) |
Add a new paragraph |
doc.text() |
Get all document text |
| Method | Description |
|---|---|
para.text() |
Get paragraph text |
para.style() |
Get style name |
para.is_heading() |
Check if heading |
para.runs() |
Iterate over runs |
para.add_run(run) |
Add a text run |
para.set_style(name) |
Set paragraph style |
| Method | Description |
|---|---|
Run::new(text) |
Create a new run |
run.text() |
Get run text |
run.bold() |
Check if bold |
run.italic() |
Check if italic |
run.set_bold(bool) |
Set bold |
run.set_italic(bool) |
Set italic |
run.set_font_size_pt(size) |
Set font size |
run.set_color(hex) |
Set text color |
| Method | Description |
|---|---|
table.rows() |
Iterate over rows |
table.row_count() |
Get row count |
table.column_count() |
Get column count |
table.cell(row, col) |
Get cell at position |
┌─────────────────────────────────────────┐
│ Public API Layer │
│ Document, Paragraph, Run, Table │
├─────────────────────────────────────────┤
│ Document Layer │
│ Body, BlockContent, Properties │
├─────────────────────────────────────────┤
│ OPC Layer │
│ Package, Part, Relationships │
├─────────────────────────────────────────┤
│ XML Layer │
│ RawXmlNode, Namespace helpers │
└─────────────────────────────────────────┘
Unlike many DOCX libraries that lose formatting and unknown elements, linch-docx-rs preserves everything:
// Original document has custom XML, tracked changes, comments, etc.
let mut doc = Document::open("complex.docx")?;
// Make your changes
doc.add_paragraph("New content");
// Save - all original elements are preserved!
doc.save("output.docx")?;
This is achieved by storing unknown XML elements as RawXmlNode during parsing and serializing them back unchanged.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see LICENSE for details.