Skip to main content

Crate docxcraft

Crate docxcraft 

Source
Expand description

§docxcraft

Generate editable Word .docx documents in Rust: paragraphs, tables, images, SVG drawings and native charts, packaged directly as Office Open XML. Microsoft Office and external services are not required to generate files.

Use this library for reports assembled from your application’s data. It is independent of the Reporsa CLI and report JSON protocol.

§Project status

Source repository: AlgForce/docxcraft. The 0.1 API is evolving. See CHANGELOG.md for version details.

The manifest declares Rust 1.89 or later, edition 2021. Recent local checks used Rust 1.93 on Windows x64. CI is configured for Linux and Windows; this is not a claim that every Office viewer or toolchain has been verified.

§Start from source

From this repository’s root directory:

cargo run --example basic
cargo run --example chart

The examples create target/examples/basic.docx and target/examples/chart.docx. This directory is ignored by Git.

Charts use xlsxcraft from crates.io. For cross-repository development, keep a sibling xlsxcraft checkout and copy .cargo/config.example.toml to ignored .cargo/config.toml. Adjust the local path if needed. This override is optional; no parent workspace is required. See development and publication for registry dependency rules.

§Install in an application

Add the following to your application’s Cargo.toml:

[dependencies]
docxcraft = "0.1.0"

For local development, use a path dependency such as docxcraft = { path = "../docxcraft" }. To test a sibling XLSX checkout, configure the following in the application’s .cargo/config.toml too:

[patch.crates-io]
xlsxcraft = { path = "../xlsxcraft" }

Cargo uses configuration from the directory where it is invoked, rather than automatically loading a dependency’s local config.

§Quick start

This complete program is also examples/basic.rs:

use docxcraft::{
    document::Document,
    paragraph::Paragraph,
    table::{Table, TableCell, TableRow},
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let locale = String::from("en");
    let mut document = Document::new(&locale);
    document.add_paragraph(Paragraph::new("Quarterly sales".into()).with_bold(true));

    let mut table = Table::new();
    let mut header = TableRow::new();
    header.add_cell(TableCell::header("Quarter"));
    header.add_cell(TableCell::header("Revenue"));
    table.add_row(header);
    let mut row = TableRow::new();
    row.add_cell(TableCell::new("Q1"));
    row.add_cell(TableCell::new("120"));
    table.add_row(row);
    document.add_table(table);

    std::fs::create_dir_all("target/examples")?;
    document.save("target/examples/basic.docx")?;
    Ok(())
}

Document appends owned content in document order and uses Word’s flow layout. Fonts and pagination are resolved by the application opening the document. save returns an error on I/O or export failure; it creates or truncates the destination file. Create parent directories and choose an appropriate output path.

§Add an editable chart

examples/chart.rs creates a native chart with an embedded XLSX data workbook. It passes an ECharts-style JSON string, with no JavaScript runtime or ECharts installation:

use docxcraft::{chart::Chart, document::Document, paragraph::Paragraph};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let locale = String::from("en");
    let mut document = Document::new(&locale);
    document.add_paragraph(Paragraph::new("Quarterly sales".into()));
    let option = r#"{
        "xAxis": { "type": "category", "data": ["Q1", "Q2"] },
        "yAxis": { "type": "value" },
        "series": [{ "name": "Revenue", "type": "bar", "data": [120, 180] }]
    }"#;
    document.add_chart(Chart::from_echart_option("Revenue", option, None)?);

    std::fs::create_dir_all("target/examples")?;
    document.save("target/examples/chart.docx")?;
    Ok(())
}

This adapter implements a subset of ECharts options. It does not render arbitrary ECharts configurations. Match category labels to numeric series values and validate your data before conversion; some missing or invalid values fall back to zero. See the agent/API guide.

§API map

TaskEntry point
Build and save a documentdocument::Document
Plain text and paragraph formattingparagraph::Paragraph
Tablestable::{Table, TableRow, TableCell}
Markdown / HTML contentmarkdown::Markdown / html_paragraph
Imagesimage::Image
Editable SVG subseteditable_svg::EditableSvgGroup
Native chartschart::{Chart, ChartType}

Imports are module-qualified; Document is not re-exported at the crate root. Generate local API documentation with cargo doc --no-deps --open. src/lib.rs defines the public modules.

§Scope and limitations

AvailableBoundary
New DOCX files with text, tables and imagesNo API for reading or editing an existing DOCX template
Native column, bar, line, pie, radar, scatter, bubble and waterfall chart modelsViewer support varies; the chart adapter is a subset
HTML, Markdown and SVG conversionSubsets, without browser CSS layout or script execution
Editable Office contentNo PDF export, Word automation or page rendering engine
Locale-based font selectionFonts must be present in the viewer; they are not bundled

Check complex drawings, charts and pagination in the target Word/WPS/LibreOffice version. ZIP/XML regression tests check package contents and data; they do not prove visual equivalence across viewers.

§Develop and contribute

cargo fmt --all -- --check
cargo test
cargo check --examples
cargo doc --no-deps

Add --offline to Cargo build/test commands when dependencies are cached. Issue reports and feature requests are welcome. External code PRs are not currently accepted while contribution terms are being established. See CONTRIBUTING.md for changes and bug reports, CHANGELOG.md for release notes, and RELEASING.md for maintainer publication steps.

For coding agents, start with AGENTS.md, then read docs/agent-guide.md. Those files describe repository boundaries, actual method signatures, ownership and chart-input conventions.

§License and origin

AGPL-3.0-only; see LICENSE. Copyright (C) 2026 ALGFORCE INC. Separate commercial licensing inquiries: founders@exceldashboard.ai. See LICENSING.md for scope, THIRD_PARTY.md for dependencies, and PROVENANCE.md for source history.

Modules§

chart
document
editable_svg
html_paragraph
image
markdown
paragraph
table