basic_usage/
basic_usage.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 basic_usage <path/to/your/presentation.pptx> <extract_images>
6
7use pptx_to_md::{PptxContainer, Result, ParserConfig, ImageHandlingMode};
8use std::env;
9use std::fs::File;
10use std::io::Write;
11use std::path::Path;
12
13fn main() -> Result<()> {
14    // Get the PPTX file path from command line arguments
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 basic_usage <path/to/presentation.pptx> <extract_images>\ncargo run --example basic_usage sample.pptx true");
20        return Ok(());
21    };
22    
23    // Tries to read if the extract_images flag is false else set to true
24    let extract_images = if args.len() > 2 {
25        !(args[2] == "false" || args[2] == "False" || args[2] == "0")
26    } else {
27        true
28    };
29    
30    println!("Processing PPTX file: {}", pptx_path);
31
32    // Use the config builder to build your config
33    let config = ParserConfig::builder()
34        .extract_images(extract_images)
35        .compress_images(true)
36        .quality(75)
37        .image_handling_mode(ImageHandlingMode::InMarkdown)
38        .include_slide_comment(true)
39        .build();
40    
41    // Open the PPTX file
42    let mut container = PptxContainer::open(Path::new(pptx_path), config)?;
43
44    // Parse all slides
45    let slides = container.parse_all()?;
46
47    println!("Found {} slides", slides.len());
48
49    // create a new Markdown file
50    let mut md_file = File::create("output.md")?;
51
52    // Convert each slide to Markdown and save
53    for slide in slides {
54        if let Some(md_content) = slide.convert_to_md() {
55            println!("{}", md_content);
56            writeln!(md_file, "{}", md_content).expect("Couldn't write to file");
57        }
58    }
59
60    println!("All slides converted successfully!");
61
62    Ok(())
63}