Skip to main content

Crate office_toolkit

Crate office_toolkit 

Source
Expand description

office-toolkit — facade crate for the toolkit.

Single entry point re-exporting the public API of the format-specific crates, each behind its own same-named Cargo feature — but all three enabled by full, which is also this crate’s own default. A plain cargo add office-toolkit compiles and works with zero configuration; the features exist for anyone who wants to opt out of formats they don’t use (smaller binary, faster build), never as a requirement to get started. Simplicity for the common case was chosen deliberately over “pay only for what you use by default”.

§Module layout

  • word — everything from word_ooxml, when the word feature is enabled (on by default).
  • excel — everything from excel_ooxml, when the excel feature is enabled (on by default).
  • powerpoint — everything from powerpoint_ooxml, when the powerpoint feature is enabled (on by default).
  • drawing/chart — the two crates shared across all three formats (shape/text formatting, embedded charts respectively) — always available, unconditionally, since every format needs them to add an image, a shape, or a chart.
  • prelude — the handful of types used constantly regardless of format (Document/Workbook/Presentation, each format’s most basic content-building type — Paragraph/Row/Slide — plus the most common drawing types), re-exported unprefixed, so use office_toolkit::prelude::*; alone is enough to start writing code.

Everything deeper stays in its own namespaced module above — that namespacing isn’t optional there: several types (DocumentProperties, Theme, ColorScheme, CustomPropertyValue) intentionally share the same name across two or three formats (kept consistent between them on purpose), so re-exporting all of them flat at this crate’s root would collide.

open auto-detects a .docx/.xlsx/.pptx file’s format from the package’s own content (not the file’s name or extension) and returns a single OfficeDocument; OpenFile/SaveToFile add open_file/save_to_file methods directly on Document/Workbook/Presentation for the more common case where the caller already knows which format they’re working with.

§Example

use office_toolkit::prelude::*;
use office_toolkit::SaveToFile;

// Build a minimal Word document and save it — `Document` and `Paragraph` both come straight
// from the prelude. `save_to_file` comes from the `SaveToFile` trait (see [`SaveToFile`]) —
// it has to be imported explicitly like any trait method, the prelude only covers types, not
// traits.
let document = Document::new().with_paragraph(Paragraph::with_text("Hello, world!"));
document.save_to_file("hello.docx")?;

// Open any .docx/.xlsx/.pptx without knowing its format ahead of time.
let opened = office_toolkit::open("hello.docx")?;
opened.save_to_file("hello-resaved.docx")?;

Modules§

chart
Everything from the chart crate — embedded charts, shared by every format. Always available, same reasoning as crate::drawing.
drawing
Everything from the drawing crate — shape/text formatting shared by every format (fill, line, geometry, text body…). Always available: unlike word/excel/powerpoint, not gated by a feature, since it has no meaningful “opt out” — any format that has images or shapes at all needs it.
excel
Everything from excel_ooxml — reading and writing .xlsx.
powerpoint
Everything from powerpoint_ooxml — reading and writing .pptx.
prelude
The handful of types needed to get started with any format, re-exported unprefixed. See the crate-level doc comment for why only these (and not the rest of the API) live here.
word
Everything from word_ooxml — reading and writing .docx.

Enums§

Error
Errors from office-toolkit’s own file-level convenience layer.
OfficeDocument
A document opened by open, with its format determined at runtime from the file’s own content rather than assumed up front. Match on this when the caller genuinely doesn’t know the format ahead of time; when it does, OpenFile::open_file on the concrete type (word::Document::open_file(...), etc.) skips the enum entirely.

Traits§

OpenFile
Reads a Document/Workbook/Presentation directly from a path on disk — word::Document::open_file("report.docx")? instead of manually opening a File and calling the type’s own read_from. For the common case where the caller already knows which format they’re working with; see open for auto-detection.
SaveToFile
Writes a Document/Workbook/Presentation directly to a path on disk — doc.save_to_file("report.docx")? instead of manually creating a File and calling the type’s own write_to.

Functions§

open
Opens a .docx/.xlsx/.pptx file, detecting its format from the OPC package’s own parts (word/document.xml/xl/workbook.xml/ ppt/presentation.xml) rather than from the file’s extension or name — a renamed or extension-less file still opens correctly, and a file carrying none of the three is reported as Error::UnrecognizedFormat rather than silently misidentified as one of them. A file whose format is recognized, but whose matching Cargo feature isn’t enabled in this build, is reported as Error::FormatNotEnabled instead — a more useful answer than pretending the file doesn’t exist.

Type Aliases§

Result
A Result alias using [Error] as the error type.