Skip to main content

edit_presentation/
edit_presentation.rs

1//! Example demonstrating PPTX editing capabilities
2//!
3//! This example shows how to:
4//! - Open existing PPTX files for editing
5//! - Add new slides to a presentation
6//! - Update existing slide content
7//! - Remove slides
8//! - Save modified presentations
9
10use ppt_rs::generator::{create_pptx_with_content, SlideContent, SlideLayout};
11use ppt_rs::oxml::{PresentationEditor, PresentationReader};
12use std::fs;
13
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    println!("╔════════════════════════════════════════════════════════════╗");
16    println!("║         PPTX Editing Demo                                  ║");
17    println!("╚════════════════════════════════════════════════════════════╝\n");
18
19    // =========================================================================
20    // Step 1: Create an initial presentation
21    // =========================================================================
22    println!("📝 Step 1: Creating initial presentation...");
23    
24    let initial_slides = vec![
25        SlideContent::new("Original Presentation")
26            .layout(SlideLayout::CenteredTitle)
27            .title_bold(true)
28            .title_color("1F497D"),
29        
30        SlideContent::new("Slide 1: Introduction")
31            .add_bullet("This is the original content")
32            .add_bullet("Created programmatically"),
33        
34        SlideContent::new("Slide 2: Features")
35            .add_bullet("Feature A")
36            .add_bullet("Feature B")
37            .add_bullet("Feature C"),
38    ];
39    
40    let pptx_data = create_pptx_with_content("Original Presentation", initial_slides)?;
41    fs::write("original.pptx", &pptx_data)?;
42    println!("   ✓ Created original.pptx with 3 slides\n");
43
44    // =========================================================================
45    // Step 2: Open and inspect the presentation
46    // =========================================================================
47    println!("📖 Step 2: Opening presentation for editing...");
48    
49    let mut editor = PresentationEditor::open("original.pptx")?;
50    println!("   ✓ Opened original.pptx");
51    println!("   ├── Slide count: {}", editor.slide_count());
52    
53    // Read first slide
54    let slide0 = editor.get_slide(0)?;
55    println!("   └── First slide title: {:?}\n", slide0.title);
56
57    // =========================================================================
58    // Step 3: Add new slides
59    // =========================================================================
60    println!("➕ Step 3: Adding new slides...");
61    
62    // Add a new slide at the end
63    let new_slide1 = SlideContent::new("New Slide: Added via Editor")
64        .add_bullet("This slide was added programmatically")
65        .add_bullet("Using PresentationEditor")
66        .add_bullet("After the presentation was created")
67        .title_color("9BBB59");
68    
69    let idx1 = editor.add_slide(new_slide1)?;
70    println!("   ✓ Added slide at index {}", idx1);
71    
72    // Add another slide
73    let new_slide2 = SlideContent::new("Another New Slide")
74        .layout(SlideLayout::TwoColumn)
75        .add_bullet("Left column item 1")
76        .add_bullet("Left column item 2")
77        .add_bullet("Right column item 1")
78        .add_bullet("Right column item 2");
79    
80    let idx2 = editor.add_slide(new_slide2)?;
81    println!("   ✓ Added slide at index {}", idx2);
82    println!("   └── Total slides now: {}\n", editor.slide_count());
83
84    // =========================================================================
85    // Step 4: Update existing slide
86    // =========================================================================
87    println!("✏️  Step 4: Updating existing slide...");
88    
89    let updated_slide = SlideContent::new("Slide 2: Updated Features")
90        .add_bullet("Feature A - Enhanced!")
91        .add_bullet("Feature B - Improved!")
92        .add_bullet("Feature C - Optimized!")
93        .add_bullet("Feature D - NEW!")
94        .title_color("C0504D")
95        .content_bold(true);
96    
97    editor.update_slide(2, updated_slide)?;
98    println!("   ✓ Updated slide at index 2\n");
99
100    // =========================================================================
101    // Step 5: Save modified presentation
102    // =========================================================================
103    println!("💾 Step 5: Saving modified presentation...");
104    
105    editor.save("modified.pptx")?;
106    println!("   ✓ Saved as modified.pptx\n");
107
108    // =========================================================================
109    // Step 6: Verify the changes
110    // =========================================================================
111    println!("🔍 Step 6: Verifying changes...");
112    
113    let reader = PresentationReader::open("modified.pptx")?;
114    println!("   Modified presentation:");
115    println!("   ├── Slide count: {}", reader.slide_count());
116    
117    for i in 0..reader.slide_count() {
118        let slide = reader.get_slide(i)?;
119        let title = slide.title.as_deref().unwrap_or("(no title)");
120        let bullets = slide.body_text.len();
121        println!("   {}── Slide {}: \"{}\" ({} bullets)", 
122                 if i == reader.slide_count() - 1 { "└" } else { "├" },
123                 i + 1, 
124                 title,
125                 bullets);
126    }
127
128    // =========================================================================
129    // Step 7: Demonstrate slide removal
130    // =========================================================================
131    println!("\n🗑️  Step 7: Demonstrating slide removal...");
132    
133    let mut editor2 = PresentationEditor::open("modified.pptx")?;
134    println!("   Before removal: {} slides", editor2.slide_count());
135    
136    // Remove the last slide
137    editor2.remove_slide(editor2.slide_count() - 1)?;
138    println!("   ✓ Removed last slide");
139    println!("   After removal: {} slides", editor2.slide_count());
140    
141    editor2.save("trimmed.pptx")?;
142    println!("   ✓ Saved as trimmed.pptx");
143
144    // Cleanup
145    fs::remove_file("original.pptx").ok();
146    fs::remove_file("modified.pptx").ok();
147    fs::remove_file("trimmed.pptx").ok();
148
149    // =========================================================================
150    // Summary
151    // =========================================================================
152    println!("\n╔════════════════════════════════════════════════════════════╗");
153    println!("║                    Demo Complete                           ║");
154    println!("╠════════════════════════════════════════════════════════════╣");
155    println!("║  Capabilities Demonstrated:                                ║");
156    println!("║  ✓ PresentationEditor::open() - Open for editing           ║");
157    println!("║  ✓ editor.add_slide() - Add new slides                     ║");
158    println!("║  ✓ editor.update_slide() - Modify existing slides          ║");
159    println!("║  ✓ editor.remove_slide() - Remove slides                   ║");
160    println!("║  ✓ editor.save() - Save modified presentation              ║");
161    println!("║  ✓ editor.get_slide() - Read slide content                 ║");
162    println!("╚════════════════════════════════════════════════════════════╝");
163
164    Ok(())
165}