slide_elements/
slide_elements.rs

1//! Working with the parsed slide elements example for the pptx-to-md crate
2//!
3//! This example demonstrates how to use the slide elements before the elements are parsed to Markdown,
4//! to do your conversion logic.
5//!
6//! Run with: cargo run --example slide_elements <path/to/your/presentation.pptx>
7
8use pptx_to_md::{ParserConfig, PptxContainer, Result, SlideElement};
9use std::env;
10use std::path::Path;
11
12fn main() -> Result<()> {
13    // Get the PPTX file path from command line arguments
14    let args: Vec<String> = env::args().collect();
15    let pptx_path = if args.len() > 1 {
16        &args[1]
17    } else {
18        eprintln!("Usage: cargo run --example slide_elements <path/to/presentation.pptx>");
19        return Ok(());
20    };
21
22    println!("Processing PPTX file: {}", pptx_path);
23
24    // Use the config builder to build your config
25    let config = ParserConfig::builder()
26        .extract_images(true)
27        .build();
28    
29    // Open the PPTX file with the streaming API
30    let mut streamer = PptxContainer::open(Path::new(pptx_path), config)?;
31
32    // Process slides one by one using the iterator
33    for slide_result in streamer.iter_slides() {
34        match slide_result {
35            Ok(slide) => {
36                println!("Processing slide {} ({} elements)", slide.slide_number, slide.elements.len());
37
38                // iterate over each slide element and match them to add custom logic
39                for element in &slide.elements {
40                    match element {
41                        SlideElement::Text(text, pos) => { println!("{:?}\t{:?}\n", text, pos) }
42                        SlideElement::Table(table, pos) => { println!("{:?}\t{:?}\n", table, pos) }
43                        SlideElement::Image(image, pos) => { println!("{:?}\t{:?}\n", image, pos) }
44                        SlideElement::List(list, pos) => { println!("{:?}\t{:?}\n", list, pos) }
45                        SlideElement::Unknown => { println!("An Unknown element was found.\n") }
46                    }
47                }
48            },
49            Err(e) => {
50                eprintln!("Error processing slide: {:?}", e);
51            }
52        }
53    }
54
55    println!("All slides processed successfully!");
56
57    Ok(())
58}