Skip to main content

memory_efficient_streaming/
memory_efficient_streaming.rs

1//! Process one PPTX or ODP slide at a time instead of retaining all slides.
2//!
3//! Run with:
4//! cargo run --example memory_efficient_streaming <presentation.pptx|presentation.odp>
5
6use pptx_to_md::{ParserConfig, PresentationContainer, Result};
7use std::env;
8use std::fs;
9use std::path::Path;
10
11fn main() -> Result<()> {
12    let args: Vec<String> = env::args().collect();
13    let Some(input_path) = args.get(1) else {
14        eprintln!(
15            "Usage: cargo run --example memory_efficient_streaming <presentation.pptx|presentation.odp>"
16        );
17        return Ok(());
18    };
19
20    let mut presentation =
21        PresentationContainer::open(Path::new(input_path), ParserConfig::default())?;
22    let output_dir = "output_streaming";
23    fs::create_dir_all(output_dir)?;
24
25    // Unlike parse_document(), the iterator only retains the current slide.
26    for slide_result in presentation.iter_slides() {
27        let slide = slide_result?;
28        let output_path = format!("{output_dir}/slide_{}.md", slide.slide_number);
29        fs::write(&output_path, slide.convert_to_md()?)?;
30        println!(
31            "Saved slide {} ({} semantic blocks) to {output_path}",
32            slide.slide_number,
33            slide.blocks.len()
34        );
35    }
36
37    Ok(())
38}