forked from pythonicrubyist/powerpoint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_test_powerpoint.rb
133 lines (109 loc) · 3.31 KB
/
generate_test_powerpoint.rb
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
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'powerpoint', path: '.' # Assuming we're in the gem's root directory
end
require 'tempfile'
def pixel_to_pt(px)
px * 12700
end
module PresentationConstants
SLIDE_WIDTH_PX = 720
DEFAULT_WIDTH_PX = 550
DEFAULT_HEIGHT_PX = 400
IMAGE_Y_OFFSET_PX = 120
SLIDE_WIDTH = pixel_to_pt(SLIDE_WIDTH_PX)
DEFAULT_WIDTH = pixel_to_pt(DEFAULT_WIDTH_PX)
DEFAULT_HEIGHT = pixel_to_pt(DEFAULT_HEIGHT_PX)
IMAGE_Y_OFFSET = pixel_to_pt(IMAGE_Y_OFFSET_PX)
end
def create_test_powerpoint
# Create a new presentation
deck = Powerpoint::Presentation.new
# Add title slide
deck.add_intro("Test Presentation", "Created with Ruby")
# Add text-only slide
deck.add_textual_slide(
"Text Slide",
["First bullet point", "Second bullet point", "Third bullet point"]
)
# Test images with their dimensions
image_files = [
{
path: "samples/images/landscape.jpeg", # Landscape 16:9
name: "Landscape Image",
width: 1920,
height: 1080
},
{
path: "samples/images/3_4.jpeg", # Portrait 3:4
name: "Portrait Image",
width: 900,
height: 1200
},
{
path: "samples/images/square.jpeg", # Square 1:1
name: "Square Image",
width: 1000,
height: 1000
}
]
# Add image slides
image_files.each do |image|
next unless File.exist?(image[:path])
image_width = pixel_to_pt(image[:width])
image_height = pixel_to_pt(image[:height])
# Calculate dimensions
if image_height > image_width && image_height > PresentationConstants::DEFAULT_HEIGHT
new_height = PresentationConstants::DEFAULT_HEIGHT
ratio = new_height / image_height.to_f
new_width = (image_width.to_f * ratio).round
else
new_width = PresentationConstants::DEFAULT_WIDTH < image_width ? PresentationConstants::DEFAULT_WIDTH : image_width
ratio = new_width / image_width.to_f
new_height = (image_height.to_f * ratio).round
end
coords = {
x: (PresentationConstants::SLIDE_WIDTH / 2) - (new_width / 2),
y: PresentationConstants::IMAGE_Y_OFFSET,
cx: new_width,
cy: new_height
}
# Add different types of slides with images
deck.add_pictorial_slide(
"#{image[:name]} - Full",
image[:path],
coords
)
deck.add_text_picture_slide(
"#{image[:name]} - With Text",
image[:path],
["Left side text", "With bullet points", "Next to image"]
)
deck.add_picture_description_slide(
"#{image[:name]} - With Description",
image[:path],
["Description below the image", "With multiple points", "For detail"]
)
end
# Save the presentation
output_dir = "output"
output_file = "test_presentation"
begin
# Ensure output directory exists
FileUtils.mkdir_p(output_dir)
# Save with base name (PowerPoint gem will append timestamp)
saved_path = deck.save("#{output_dir}/#{output_file}")
# Rename the file to remove timestamp
final_path = "#{output_dir}/#{output_file}.pptx"
FileUtils.mv(saved_path, final_path)
puts "Presentation saved to: #{final_path}"
rescue StandardError => e
puts "Error saving presentation: #{e.message}"
puts "Stack trace:"
puts e.backtrace
exit 1
end
end
# Run the test
create_test_powerpoint