ppt-rs 0.2.14

Create, read, and update PowerPoint 2007+ (.pptx) files with rich formatting, bullet styles, themes, and templates.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! PPTX CLI - Command-line tool for creating PowerPoint presentations

use clap::Parser;
use ppt_rs::api::Presentation;
use ppt_rs::cli::{
    Cli, Commands, CreateCommand, ExportFormat, FromHtmlCommand, FromMarkdownCommand, InfoCommand,
    ValidateCommand,
};

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::Create {
            output,
            title,
            slides,
            template,
        } => match CreateCommand::execute(&output, title.as_deref(), slides, template.as_deref()) {
            Ok(_) => {
                println!("✓ Created presentation: {output}");
                let title = title.as_deref().unwrap_or("Presentation");
                println!("  Title: {title}");
                println!("  Slides: {slides}");
            }
            Err(e) => {
                eprintln!("✗ Error: {e}");
                std::process::exit(1);
            }
        },
        Commands::Md2Ppt {
            input,
            output,
            title,
        } => {
            // Auto-generate output if not provided
            let output_path = output.unwrap_or_else(|| {
                use std::path::Path;
                let input_path = Path::new(&input);
                if let Some(stem) = input_path.file_stem() {
                    if let Some(parent) = input_path.parent() {
                        if parent.as_os_str().is_empty() {
                            format!("{}.pptx", stem.to_string_lossy())
                        } else {
                            format!("{}/{}.pptx", parent.display(), stem.to_string_lossy())
                        }
                    } else {
                        format!("{}.pptx", stem.to_string_lossy())
                    }
                } else {
                    format!("{}.pptx", input)
                }
            });

            match FromMarkdownCommand::execute(&input, &output_path, title.as_deref()) {
                Ok(_) => {
                    println!("✓ Created presentation: {output_path}");
                    println!("  Input: {input}");
                    let title = title.as_deref().unwrap_or("Presentation from Markdown");
                    println!("  Title: {title}");
                }
                Err(e) => {
                    eprintln!("✗ Error: {e}");
                    std::process::exit(1);
                }
            }
        }
        Commands::Info { file } => match InfoCommand::execute(&file) {
            Ok(_) => {}
            Err(e) => {
                eprintln!("✗ Error: {e}");
                std::process::exit(1);
            }
        },
        Commands::Validate { file } => match ValidateCommand::execute(&file) {
            Ok(_) => {
                println!("\n✓ Validation completed successfully");
            }
            Err(e) => {
                eprintln!("✗ Error: {e}");
                std::process::exit(1);
            }
        },
        Commands::Export {
            input,
            output,
            format,
        } => {
            println!("Exporting {}...", input);
            let pres = match Presentation::from_path(&input) {
                Ok(p) => p,
                Err(e) => {
                    eprintln!("✗ Error loading presentation: {}", e);
                    std::process::exit(1);
                }
            };

            let format = format
                .or_else(|| {
                    let out_lower = output.to_lowercase();
                    if out_lower.ends_with(".pdf") {
                        Some(ExportFormat::Pdf)
                    } else if out_lower.ends_with(".html") {
                        Some(ExportFormat::Html)
                    } else if out_lower.ends_with(".png") {
                        Some(ExportFormat::Png)
                    } else {
                        None
                    }
                })
                .unwrap_or(ExportFormat::Pdf);

            let result = match format {
                ExportFormat::Pdf => pres.save_as_pdf(&output),
                ExportFormat::Html => pres.save_as_html(&output),
                ExportFormat::Png => pres.save_as_png(&output),
            };

            match result {
                Ok(_) => println!("✓ Export completed: {}", output),
                Err(e) => {
                    eprintln!("✗ Error exporting: {}", e);
                    std::process::exit(1);
                }
            }
        }
        Commands::Merge { output, inputs } => {
            println!("Merging {} files into {}...", inputs.len(), output);
            let mut final_pres = match Presentation::from_path(&inputs[0]) {
                Ok(p) => p,
                Err(e) => {
                    eprintln!("✗ Error loading {}: {}", inputs[0], e);
                    std::process::exit(1);
                }
            };

            for input in inputs.iter().skip(1) {
                match Presentation::from_path(input) {
                    Ok(p) => {
                        final_pres = final_pres.add_presentation(p);
                    }
                    Err(e) => {
                        eprintln!("✗ Error loading {}: {}", input, e);
                        std::process::exit(1);
                    }
                }
            }

            match final_pres.save(&output) {
                Ok(_) => println!("✓ Merge completed: {}", output),
                Err(e) => {
                    eprintln!("✗ Error saving merged file: {}", e);
                    std::process::exit(1);
                }
            }
        }
        Commands::Html2Ppt {
            input,
            output,
            title,
            max_slides,
            max_bullets,
            no_images,
            no_tables,
            no_code,
        } => {
            // Auto-generate output if not provided
            let output_path = output.unwrap_or_else(|| {
                use std::path::Path;
                let input_path = Path::new(&input);
                if let Some(stem) = input_path.file_stem() {
                    if let Some(parent) = input_path.parent() {
                        if parent.as_os_str().is_empty() {
                            format!("{}.pptx", stem.to_string_lossy())
                        } else {
                            format!("{}/{}.pptx", parent.display(), stem.to_string_lossy())
                        }
                    } else {
                        format!("{}.pptx", stem.to_string_lossy())
                    }
                } else {
                    format!("{}.pptx", input)
                }
            });

            match FromHtmlCommand::execute(
                &input,
                &output_path,
                title.as_deref(),
                max_slides,
                max_bullets,
                no_images,
                no_tables,
                no_code,
            ) {
                Ok(_) => {
                    println!("✓ Created presentation: {output_path}");
                    println!("  Input: {input}");
                    let title = title.as_deref().unwrap_or("Presentation from HTML");
                    println!("  Title: {title}");
                }
                Err(e) => {
                    eprintln!("✗ Error: {e}");
                    std::process::exit(1);
                }
            }
        }
        Commands::Pdf2Ppt { input, output } => {
            let output = output.unwrap_or_else(|| {
                let path = std::path::Path::new(&input);
                path.with_extension("pptx").to_string_lossy().to_string()
            });

            println!("Converting {} to {}...", input, output);
            match Presentation::from_pdf(&input) {
                Ok(p) => match p.save(&output) {
                    Ok(_) => println!("✓ Conversion completed: {}", output),
                    Err(e) => {
                        eprintln!("✗ Error saving presentation: {}", e);
                        std::process::exit(1);
                    }
                },
                Err(e) => {
                    eprintln!("✗ Error converting PDF: {}", e);
                    std::process::exit(1);
                }
            }
        }
        #[cfg(feature = "web2ppt")]
        Commands::Web2Ppt {
            url,
            output,
            title,
            max_slides,
            max_bullets,
            no_images,
            no_tables,
            no_code,
            no_source_url,
            timeout,
            verbose,
        } => {
            execute_web2ppt(
                url,
                output,
                title,
                max_slides,
                max_bullets,
                no_images,
                no_tables,
                no_code,
                no_source_url,
                timeout,
                verbose,
            );
        }
    }
}

#[cfg(feature = "web2ppt")]
fn execute_web2ppt(
    url: String,
    output: String,
    title: Option<String>,
    max_slides: usize,
    max_bullets: usize,
    no_images: bool,
    no_tables: bool,
    no_code: bool,
    no_source_url: bool,
    timeout: u64,
    verbose: bool,
) {
    use ppt_rs::{ConversionOptions, Web2Ppt, Web2PptConfig, WebFetcher, WebParser};

    if verbose {
        println!("🌐 Web2PPT - Converting webpage to PowerPoint");
        println!("   URL: {}", url);
        println!("   Output: {}", output);
        println!("   Max slides: {}", max_slides);
        println!("   Max bullets: {}", max_bullets);
        println!("   Include images: {}", !no_images);
        println!("   Include tables: {}", !no_tables);
        println!("   Include code: {}", !no_code);
        println!();
        println!("📥 Fetching webpage...");
    }

    // Build config
    let config = Web2PptConfig::new()
        .max_slides(max_slides)
        .max_bullets(max_bullets)
        .with_images(!no_images)
        .with_tables(!no_tables)
        .with_code(!no_code)
        .timeout(timeout);

    // Build options
    let mut options = ConversionOptions::new().with_source_url(!no_source_url);

    if let Some(t) = title.as_ref() {
        options = options.title(t);
    }

    // Fetch
    let fetcher = match WebFetcher::with_config(config.clone()) {
        Ok(f) => f,
        Err(e) => {
            eprintln!("✗ Failed to create fetcher: {}", e);
            std::process::exit(1);
        }
    };

    let html = match fetcher.fetch(&url) {
        Ok(h) => {
            if verbose {
                println!("   Fetched {} bytes of HTML", h.len());
            }
            h
        }
        Err(e) => {
            eprintln!("✗ Failed to fetch: {}", e);
            std::process::exit(1);
        }
    };

    // Parse
    let parser = WebParser::with_config(config.clone());
    let content = match parser.parse(&html, &url) {
        Ok(c) => {
            if verbose {
                println!("📄 Parsed content:");
                println!("   Title: {}", c.title);
                println!(
                    "   Description: {}",
                    c.description.as_deref().unwrap_or("(none)")
                );
                println!("   Content blocks: {}", c.blocks.len());
                println!("   Images: {}", c.images.len());

                // Show headings found
                let headings: Vec<_> = c
                    .blocks
                    .iter()
                    .filter(|b| b.is_heading())
                    .map(|b| b.text.as_str())
                    .take(10)
                    .collect();
                if !headings.is_empty() {
                    println!("   Headings found: {:?}", headings);
                }
            }
            c
        }
        Err(e) => {
            eprintln!("✗ Failed to parse: {}", e);
            std::process::exit(1);
        }
    };

    // Convert
    let converter = Web2Ppt::with_config(config);
    match converter.convert(&content, &options) {
        Ok(pptx_data) => {
            if verbose {
                println!("✅ Conversion successful!");
                println!("   Size: {} bytes", pptx_data.len());
                println!("💾 Saving to {}...", output);
            }

            match std::fs::write(&output, &pptx_data) {
                Ok(_) => {
                    println!("✓ Created: {} ({} bytes)", output, pptx_data.len());
                }
                Err(e) => {
                    eprintln!("✗ Failed to save file: {}", e);
                    std::process::exit(1);
                }
            }
        }
        Err(e) => {
            eprintln!("✗ Conversion failed: {}", e);
            std::process::exit(1);
        }
    }
}

#[cfg(not(feature = "web2ppt"))]
#[allow(dead_code)]
fn execute_web2ppt(
    _url: String,
    _output: String,
    _title: Option<String>,
    _max_slides: usize,
    _max_bullets: usize,
    _no_images: bool,
    _no_tables: bool,
    _no_code: bool,
    _no_source_url: bool,
    _timeout: u64,
    _verbose: bool,
) {
    eprintln!("✗ web2ppt feature is not enabled.");
    eprintln!("  Rebuild with: cargo build --features web2ppt");
    std::process::exit(1);
}