office-toolkit 1.0.0

A Rust library to create, read, and modify Microsoft Office documents (Word/Excel/PowerPoint) in the modern Open XML (OOXML) format.
Documentation
//! The simplest possible Word document: one paragraph, saved to a real
//! `.docx` file, then read back to prove the file this crate wrote is
//! valid and round-trips correctly.
//!
//! Run with: `cargo run -p office-toolkit --example hello_world_docx`

use std::path::{Path, PathBuf};

use office_toolkit::prelude::*;
use office_toolkit::{OpenFile, SaveToFile};

fn main() -> office_toolkit::Result<()> {
    let path = output_path("hello_world.docx");

    // `Document` and `Paragraph` both come from `office_toolkit::prelude`.
    let document = Document::new().with_paragraph(Paragraph::with_text("Hello, world!"));
    document.save_to_file(&path)?;
    println!("Wrote {}", path.display());

    // Read the file back to confirm it is a valid .docx.
    let reopened = Document::open_file(&path)?;
    let text = reopened
        .paragraphs()
        .next()
        .map(|paragraph| paragraph.text());
    println!("Read back: {text:?}");

    Ok(())
}

/// Every example in this directory writes its output under
/// `tests-data/output/`, resolved relative to this crate's own manifest so
/// it works no matter what directory `cargo run` was invoked from.
fn output_path(filename: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../../tests-data/output")
        .join(filename)
}