Skip to main content

hello_world_pptx/
hello_world_pptx.rs

1//! The simplest possible PowerPoint presentation: one slide, one text
2//! shape, saved to a real `.pptx` file, then read back to prove the file
3//! this crate wrote is valid and round-trips correctly.
4//!
5//! Run with: `cargo run -p office-toolkit --example hello_world_pptx`
6
7use std::path::{Path, PathBuf};
8
9use office_toolkit::drawing::{ShapeProperties, TextBody, TextParagraph, TextRun, Transform2D};
10use office_toolkit::powerpoint::{AutoShape, Shape};
11use office_toolkit::prelude::*;
12use office_toolkit::{OpenFile, SaveToFile};
13
14fn main() -> office_toolkit::Result<()> {
15    let path = output_path("hello_world.pptx");
16
17    let text_box = AutoShape::new(2, "Hello box")
18        .with_properties(
19            ShapeProperties::new().with_transform(
20                Transform2D::new()
21                    .with_offset(838_200, 1_365_250)
22                    .with_extent(7_772_400, 1_200_150),
23            ),
24        )
25        .with_text_body(
26            TextBody::new()
27                .with_paragraph(TextParagraph::new().with_run(TextRun::text("Hello, world!"))),
28        );
29
30    // `Presentation` and `Slide` both come from `office_toolkit::prelude`.
31    let presentation =
32        Presentation::new().with_slide(Slide::new().with_shape(Shape::AutoShape(text_box)));
33    presentation.save_to_file(&path)?;
34    println!("Wrote {}", path.display());
35
36    // Read the file back to confirm it is a valid .pptx.
37    let reopened = Presentation::open_file(&path)?;
38    println!("Slide count: {}", reopened.slides.len());
39
40    Ok(())
41}
42
43/// Every example in this directory writes its output under
44/// `tests-data/output/`, resolved relative to this crate's own manifest so
45/// it works no matter what directory `cargo run` was invoked from.
46fn output_path(filename: &str) -> PathBuf {
47    Path::new(env!("CARGO_MANIFEST_DIR"))
48        .join("../../tests-data/output")
49        .join(filename)
50}