linch-docx-rs
A reliable DOCX reading and writing library for Rust with round-trip preservation.
Features
- Read & Write DOCX - Full support for reading and creating Word documents
- Round-trip Preservation - Unknown elements are kept intact during read-modify-save operations
- Simple API - Pythonic API design inspired by python-docx
- Type Safe - Leverages Rust's type system for reliability
- Zero Unsafe - Pure safe Rust implementation
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
Reading a Document
use Document;
Creating a Document
use ;
Modifying an Existing Document
use Document;
API Overview
Document
| 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 |
Paragraph
| 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 |
Run (Text with Formatting)
| 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 |
Table
| 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 |
Architecture
┌─────────────────────────────────────────┐
│ Public API Layer │
│ Document, Paragraph, Run, Table │
├─────────────────────────────────────────┤
│ Document Layer │
│ Body, BlockContent, Properties │
├─────────────────────────────────────────┤
│ OPC Layer │
│ Package, Part, Relationships │
├─────────────────────────────────────────┤
│ XML Layer │
│ RawXmlNode, Namespace helpers │
└─────────────────────────────────────────┘
Round-trip Preservation
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 = open?;
// Make your changes
doc.add_paragraph;
// Save - all original elements are preserved!
doc.save?;
This is achieved by storing unknown XML elements as RawXmlNode during parsing and serializing them back unchanged.
Roadmap
- Basic document reading
- Basic document writing
- Paragraph and Run support
- Table support (read)
- Round-trip preservation
- Images and drawings
- Headers and footers
- Styles management
- Table creation/modification
- Lists and numbering
- Comments and track changes
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE for details.
Acknowledgments
- Inspired by python-docx
- Built with quick-xml and zip