-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresent_ai.py
416 lines (334 loc) · 14.9 KB
/
present_ai.py
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
407
408
409
410
411
412
413
414
415
416
from pptx import Presentation
from pptx.dml.color import RGBColor
from pptx.util import Pt
from text_gen import get_response
from image import get_image
import io
from io import BytesIO
def get_presentation(topic):
slide1_title, slide2_content, slide_t, slide_c = get_response(topic)
presentation = Presentation()
ppt_bytes = BytesIO()
get_image(topic)
slide0 = presentation.slides.add_slide(presentation.slide_layouts[0])
title0 = slide0.shapes.title
title0.text = slide1_title
title0.text_frame.paragraphs[0].runs[0].font.bold = True
subtitle_placeholder = slide0.placeholders[1]
subtitle_placeholder.text = ""
slide0.background.fill.solid()
slide0.background.fill.fore_color.rgb = RGBColor(0, 0, 0)
for shape in slide0.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(255, 255, 255)
title0.text_frame.paragraphs[0].runs[0].font.size = Pt(80)
# Slide 1
slide1 = presentation.slides.add_slide(presentation.slide_layouts[3])
title1 = slide1.shapes.title
title1.text = slide_t[0]
title1.text_frame.paragraphs[0].runs[0].font.bold = True
content1 = slide1.placeholders[1]
content1.text = slide_c[0]
slide1.background.fill.solid()
slide1.background.fill.fore_color.rgb = RGBColor(0, 0, 0)
#
for shape in slide1.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(255, 255, 255)
run.font.size = Pt(18)
title1.text_frame.paragraphs[0].runs[0].font.size = Pt(40)
image_placeholder = slide1.placeholders[2]
for shape in slide1.shapes:
if shape == image_placeholder:
slide1.shapes._spTree.remove(shape._element)
# Add the image to the placeholder
image_path1 = 'images/1.jpg'
left1 = image_placeholder.left
top1 = image_placeholder.top
width1 = image_placeholder.width
height1 = image_placeholder.height
# Calculate the aspect ratio of the image
image_ratio = width1 / height1
# Load the image to get its original dimensions
image = slide1.shapes.add_picture(image_path1, left1, top1)
original_width = image.width
original_height = image.height
# Calculate the new dimensions to fit within the placeholder
if image_ratio > 1:
new_width = width1
new_height = int(new_width / image_ratio)
else:
new_height = height1
new_width = int(new_height * image_ratio)
# Calculate the offset to center the image within the placeholder
left_offset = (width1 - new_width) // 2
top_offset = (height1 - new_height) // 2
# Remove the temporary image shape
slide1.shapes._spTree.remove(image._element)
# Add the resized image to the placeholder
slide1.shapes.add_picture(image_path1, left1 + left_offset, top1 + top_offset, new_width, new_height)
# Slide 2
slide2 = presentation.slides.add_slide(presentation.slide_layouts[3])
title2 = slide2.shapes.title
title2.text = slide_t[1]
content2 = slide2.placeholders[1]
content2.text = slide_c[1]
slide2.background.fill.solid()
slide2.background.fill.fore_color.rgb = RGBColor(0, 0, 0)
#
for shape in slide2.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(255, 255, 255) # RGB values for white
run.font.size = Pt(18)
title2.text_frame.paragraphs[0].runs[0].font.size = Pt(40)
title2.text_frame.paragraphs[0].runs[0].font.bold = True
image_placeholder = slide2.placeholders[2]
for shape in slide2.shapes:
if shape == image_placeholder:
slide2.shapes._spTree.remove(shape._element)
image_path2 = 'images/2.jpg'
left2 = image_placeholder.left
top2 = image_placeholder.top
width2 = image_placeholder.width
height2 = image_placeholder.height
# Calculate the aspect ratio of the image
image_ratio = width2 / height2
# Load the image to get its original dimensions
image = slide2.shapes.add_picture(image_path2, left2, top2)
original_width = image.width
original_height = image.height
# Calculate the new dimensions to fit within the placeholder
if image_ratio > 1:
new_width = width2
new_height = int(new_width / image_ratio)
else:
new_height = height2
new_width = int(new_height * image_ratio)
# Calculate the offset to center the image within the placeholder
left_offset = (width2 - new_width) // 2
top_offset = (height2 - new_height) // 2
# Remove the temporary image shape
slide2.shapes._spTree.remove(image._element)
# Add the resized image to the placeholder
slide2.shapes.add_picture(image_path2, left1 + left_offset, top1 + top_offset, new_width, new_height)
slide3 = presentation.slides.add_slide(presentation.slide_layouts[3])
title3 = slide3.shapes.title
title3.text = slide_t[2]
content3 = slide3.placeholders[1]
content3.text = slide_c[2]
slide3.background.fill.solid()
slide3.background.fill.fore_color.rgb = RGBColor(0, 0, 0)
#
for shape in slide3.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(255, 255, 255) # RGB values for white
run.font.size = Pt(18)
title3.text_frame.paragraphs[0].runs[0].font.size = Pt(50)
title3.text_frame.paragraphs[0].runs[0].font.bold = True
image_placeholder = slide3.placeholders[2]
for shape in slide3.shapes:
if shape == image_placeholder:
slide3.shapes._spTree.remove(shape._element)
image_placeholder3 = slide3.placeholders[1]
image_path3 = 'images/3.jpg'
left3 = image_placeholder3.left
top3 = image_placeholder3.top
width3 = image_placeholder3.width
height3 = image_placeholder3.height
# Calculate the aspect ratio of the image
image_ratio3 = width3 / height3
# Load the image to get its original dimensions
image3 = slide3.shapes.add_picture(image_path3, left3, top3)
original_width3 = image3.width
original_height3 = image3.height
# Calculate the new dimensions to fit within the placeholder
if image_ratio3 > 1:
new_width3 = width3
new_height3 = int(new_width3 / image_ratio3)
else:
new_height3 = height3
new_width3 = int(new_height3 * image_ratio3)
# Calculate the offset to center the image within the placeholder
left_offset3 = (width3 - new_width3) // 2
top_offset3 = (height3 - new_height3) // 2
# Remove the temporary image shape
slide3.shapes._spTree.remove(image3._element)
# Add the resized image to the placeholder
slide3.shapes.add_picture(image_path3, left1 + left_offset, top1 + top_offset, new_width, new_height)
slide4 = presentation.slides.add_slide(presentation.slide_layouts[3])
title4 = slide4.shapes.title
title4.text = slide_t[3]
content4 = slide4.placeholders[1]
content4.text = slide_c[3]
slide4.background.fill.solid()
slide4.background.fill.fore_color.rgb = RGBColor(0, 0, 0)
#
for shape in slide4.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(255, 255, 255) # RGB values for white
run.font.size = Pt(18)
title4.text_frame.paragraphs[0].runs[0].font.size = Pt(40)
title4.text_frame.paragraphs[0].runs[0].font.bold = True
image_placeholder4 = slide4.placeholders[1]
image_path4 = 'images/4.jpg'
left4 = image_placeholder4.left
top4 = image_placeholder4.top
width4 = image_placeholder4.width
height4 = image_placeholder4.height
# Calculate the aspect ratio of the image
image_ratio4 = width4 / height4
# Load the image to get its original dimensions
image4 = slide4.shapes.add_picture(image_path4, left4, top4)
original_width4 = image4.width
original_height4 = image4.height
# Calculate the new dimensions to fit within the placeholder
if image_ratio4 > 1:
new_width4 = width4
new_height4 = int(new_width4 / image_ratio4)
else:
new_height4 = height4
new_width4 = int(new_height4 * image_ratio4)
# Calculate the offset to center the image within the placeholder
left_offset4 = (width4 - new_width4) // 2
top_offset4 = (height4 - new_height4) // 2
# Remove the temporary image shape
slide4.shapes._spTree.remove(image4._element)
slide4.shapes.add_picture(image_path4, left1 + left_offset, top1 + top_offset, new_width, new_height)
slide5 = presentation.slides.add_slide(presentation.slide_layouts[3])
title5 = slide5.shapes.title
title5.text = slide_t[4]
content5 = slide5.placeholders[1]
content5.text = slide_c[4]
slide5.background.fill.solid()
slide5.background.fill.fore_color.rgb = RGBColor(0, 0, 0)
#
for shape in slide5.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(255, 255, 255) # RGB values for white
run.font.size = Pt(18)
title5.text_frame.paragraphs[0].runs[0].font.size = Pt(40)
title5.text_frame.paragraphs[0].runs[0].font.bold = True
image_placeholder5 = slide5.placeholders[1]
image_path5 = 'images/5.jpg'
left5 = image_placeholder5.left
top5 = image_placeholder5.top
width5 = image_placeholder5.width
height5 = image_placeholder5.height
# Calculate the aspect ratio of the image
image_ratio5 = width5 / height5
# Load the image to get its original dimensions
image5 = slide5.shapes.add_picture(image_path5, left5, top5)
original_width5 = image5.width
original_height5 = image5.height
# Calculate the new dimensions to fit within the placeholder
if image_ratio5 > 1:
new_width5 = width5
new_height5 = int(new_width5 / image_ratio5)
else:
new_height5 = height5
new_width5 = int(new_height5 * image_ratio5)
# Calculate the offset to center the image within the placeholder
left_offset5 = (width5 - new_width5) // 2
top_offset5 = (height5 - new_height5) // 2
# Remove the temporary image shape
slide5.shapes._spTree.remove(image5._element)
slide5.shapes.add_picture(image_path5, left1 + left_offset, top1 + top_offset, new_width, new_height)
slide7 = presentation.slides.add_slide(presentation.slide_layouts[3])
title7 = slide7.shapes.title
title7.text = slide_t[5]
content7 = slide7.placeholders[1]
content7.text = slide_c[5]
slide7.background.fill.solid()
slide7.background.fill.fore_color.rgb = RGBColor(0, 0, 0)
#
for shape in slide7.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(255, 255, 255) # RGB values for white
run.font.size = Pt(18)
title7.text_frame.paragraphs[0].runs[0].font.size = Pt(40)
title7.text_frame.paragraphs[0].runs[0].font.bold = True
image_placeholder7 = slide7.placeholders[1]
image_path7 = 'images/7.jpg'
left7 = image_placeholder7.left
top7 = image_placeholder7.top
width7 = image_placeholder7.width
height7 = image_placeholder7.height
# Calculate the aspect ratio of the image
image_ratio7 = width7 / height7
# Load the image to get its original dimensions
image7 = slide7.shapes.add_picture(image_path7, left7, top7)
original_width7 = image7.width
original_height7 = image7.height
# Calculate the new dimensions to fit within the placeholder
if image_ratio7 > 1:
new_width7 = width7
new_height7 = int(new_width7 / image_ratio7)
else:
new_height7 = height7
new_width7 = int(new_height7 * image_ratio7)
# Calculate the offset to center the image within the placeholder
left_offset7 = (width7 - new_width7) // 2
top_offset7 = (height7 - new_height7) // 2
# Remove the temporary image shape
slide7.shapes._spTree.remove(image7._element)
slide7.shapes.add_picture(image_path7, left1 + left_offset, top1 + top_offset, new_width, new_height)
slide8 = presentation.slides.add_slide(presentation.slide_layouts[3])
title8 = slide8.shapes.title
title8.text = slide_t[6]
content8 = slide8.placeholders[1]
content8.text = slide_c[6]
slide8.background.fill.solid()
slide8.background.fill.fore_color.rgb = RGBColor(0, 0, 0)
#
for shape in slide8.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(255, 255, 255) # RGB values for white
run.font.size = Pt(18)
title8.text_frame.paragraphs[0].runs[0].font.size = Pt(40)
title8.text_frame.paragraphs[0].runs[0].font.bold = True
image_placeholder8 = slide8.placeholders[1]
image_path8 = 'images/8.jpg'
left8 = image_placeholder8.left
top8 = image_placeholder8.top
width8 = image_placeholder8.width
height8 = image_placeholder8.height
# Calculate the aspect ratio of the image
image_ratio8 = width8 / height8
# Load the image to get its original dimensions
image8 = slide8.shapes.add_picture(image_path8, left8, top8)
original_width8 = image8.width
original_height8 = image8.height
# Calculate the new dimensions to fit within the placeholder
if image_ratio8 > 1:
new_width8 = width8
new_height8 = int(new_width8 / image_ratio8)
else:
new_height8 = height8
new_width8 = int(new_height8 * image_ratio8)
# Calculate the offset to center the image within the placeholder
left_offset8 = (width8 - new_width8) // 2
top_offset8 = (height8 - new_height8) // 2
# Remove the temporary image shape
slide8.shapes._spTree.remove(image8._element)
slide8.shapes.add_picture(image_path8, left1 + left_offset, top1 + top_offset, new_width, new_height)
# Save the presentation
presentation.save(ppt_bytes)
# Reset the file pointer of the BytesIO object to the beginning
ppt_bytes.seek(0)
return ppt_bytes