save_images/
save_images.rs

1//! Basic usage example for the pptx-to-md crate
2//!
3//! This example demonstrates how to open a PPTX file and convert all slides to Markdown.
4//!
5//! Run with: cargo run --example save_images <path/to/your/presentation.pptx>
6
7use pptx_to_md::{ImageHandlingMode, ParserConfig, PptxContainer, Result};
8use std::fs::File;
9use std::io::Write;
10use std::path::{Path, PathBuf};
11use std::env;
12
13fn main() -> Result<()> {
14    // Get the PPTX file path from command line arguments and provide the mandatory output path
15    let args: Vec<String> = env::args().collect();
16    let pptx_path = if args.len() > 1 {
17        &args[1]
18    } else {
19        eprintln!("Usage: cargo run --example save_images <path/to/presentation.pptx>");
20        return Ok(());
21    };
22
23    println!("Processing PPTX file: {}", pptx_path);
24
25    // Use the config builder to build your config
26    let config = ParserConfig::builder()
27        .extract_images(true)
28        .compress_images(true)
29        .quality(75)
30        .image_handling_mode(ImageHandlingMode::Save)
31        .image_output_path(PathBuf::from("C:/Users/nilsk/Downloads/extracted_images"))
32        .build();
33
34    // Open the PPTX file
35    let mut container = PptxContainer::open(Path::new(pptx_path), config)?;
36
37    // Parse all slides
38    let slides = container.parse_all()?;
39
40    println!("Found {} slides", slides.len());
41
42    // create a new Markdown file
43    let mut md_file = File::create("output.md")?;
44
45    // Convert each slide to Markdown and save the images automatically
46    for slide in slides {
47        if let Some(md_content) = slide.convert_to_md() {
48            writeln!(md_file, "{}", md_content).expect("Couldn't write to file");
49        }
50    }
51
52    println!("All slides converted successfully!");
53
54    Ok(())
55}