-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPT_generator.py
328 lines (280 loc) · 14.9 KB
/
PPT_generator.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
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.shapes import MSO_SHAPE_TYPE
from pptx.enum.text import PP_ALIGN
from pptx.dml.color import RGBColor
import json
import os
class Element:
def __init__(self, content, style, bounding_box):
self.content = content
self.style = style
self.bounding_box = bounding_box
def apply_font_style_on_run(self, run):
if 'font_name' in self.style:
run.font.name = self.style['font_name']
if 'font_size' in self.style:
run.font.size = Pt(self.style['font_size'])
if 'font_color' in self.style:
run.font.color.rgb = RGBColor(self.style['font_color']['r'], self.style['font_color']['g'], self.style['font_color']['b'])
if 'bold' in self.style:
run.font.bold = self.style['bold']
if 'italics' in self.style:
run.font.italic = self.style['italics']
if 'underlined' in self.style:
run.font.underline = self.style['underlined']
def apply_font_style(self, shape):
if 'font_name' in self.style:
shape.text_frame.paragraphs[0].font.name = self.style['font_name']
if 'font_size' in self.style:
shape.text_frame.paragraphs[0].font.size = Pt(self.style['font_size'])
if 'font_color' in self.style:
shape.text_frame.paragraphs[0].font.color.rgb = RGBColor(self.style['font_color']['r'], self.style['font_color']['g'], self.style['font_color']['b'])
if 'bold' in self.style:
shape.text_frame.paragraphs[0].font.bold = self.style['bold']
else:
shape.text_frame.paragraphs[0].font.bold = False
if 'italics' in self.style:
shape.text_frame.paragraphs[0].font.italic = self.style['italics']
else:
shape.text_frame.paragraphs[0].font.italic = False
if 'underlined' in self.style:
shape.text_frame.paragraphs[0].font.underline = self.style['underlined']
else:
shape.text_frame.paragraphs[0].font.underline = False
def position_element(self, shape):
shape.left = Inches(self.bounding_box[0])
shape.top = Inches(self.bounding_box[1])
shape.width = Inches(self.bounding_box[2])
shape.height = Inches(self.bounding_box[3])
if shape.has_text_frame:
shape.text_frame.margin_left = Pt(10)
shape.text_frame.margin_right = Pt(10)
shape.text_frame.margin_top = Pt(10)
shape.text_frame.margin_bottom = Pt(10)
def render(self, slide):
raise NotImplementedError("Subclasses must implement this method")
class Title(Element):
def __init__(self, content, style, bounding_box):
super().__init__(content, style, bounding_box)
def render(self, slide):
title_shape = slide.shapes.title
title_shape.text = self.content
self.apply_font_style(title_shape)
self.position_element(title_shape)
title_shape.text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
def clean_up(self, slide):
pass
class Description(Element):
def __init__(self, content, style, bounding_box):
super().__init__(content, style, bounding_box)
def render(self, slide):
left, top, width, height = self.bounding_box
textbox = slide.shapes.add_textbox(Inches(left), Inches(top), Inches(width), Inches(height))
textbox.text = self.content
self.apply_font_style(textbox)
textbox.text_frame.auto_size = True
textbox.text_frame.word_wrap = True
self.position_element(textbox)
self.textbox = textbox
class Enumeration(Description):
def __init__(self, content, style, bounding_box, heading):
super().__init__(content, style, bounding_box)
self.heading = heading
def render(self, slide):
left, top, width, height = self.heading['xmin'], self.heading['ymin'], self.heading['width'], self.heading['height']
enum_heading = slide.shapes.add_textbox(Inches(left), Inches(top), Inches(width), Inches(height))
enum_heading.text = self.heading['value']
self.apply_font_style(enum_heading)
enum_heading.text_frame.auto_size = True
enum_heading.text_frame.word_wrap = True
enum_shape = slide.shapes.placeholders[1]
enum_tf = enum_shape.text_frame
if self.content != []:
enum_tf.text = self.content[0]
self.apply_font_style(enum_shape)
for i, pt_text in enumerate(self.content):
if i>0:
if isinstance(pt_text, str):
p = enum_tf.add_paragraph()
run = p.add_run()
run.text = pt_text
self.apply_font_style_on_run(run)
elif isinstance(pt_text, list):
for sub_pt in pt_text:
s_p = enum_tf.add_paragraph()
s_p.level = 1
sub_run = s_p.add_run()
sub_run.text = sub_pt
self.apply_font_style_on_run(run)
else:
raise Exception("Invalid Enumeration format")
enum_tf.auto_size = True
enum_tf.word_wrap = True
self.position_element(enum_shape)
self.enum_tf = enum_tf
class Figure(Element):
def __init__(self, content, style, bounding_box, caption):
super().__init__(content, style, bounding_box)
self.caption = caption
def render(self, slide):
left, top, width, height = self.bounding_box
img = slide.shapes.add_picture(self.content, Inches(left), Inches(top), Inches(width), Inches(height))
left, top, width, height = self.caption['xmin'], self.caption['ymin'], self.caption['width'], self.caption['height']
cap_shape = slide.shapes.add_textbox(Inches(left), Inches(top), Inches(width), Inches(height))
cap_shape.text = self.caption['value']
self.apply_font_style(cap_shape)
cap_shape.text_frame.auto_size = True
cap_shape.text_frame.word_wrap = True
cap_shape.text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
# original_width, original_height = img.image.size
# # Convert dimensions from pixels to inches
# img.width = Inches(original_width * scale_factor / img.image.dpi[0])
# img.height = Inches(original_height * scale_factor / img.image.dpi[1])
self.image = img
class Equation(Element):
def __init__(self, content, style, bounding_box):
super().__init__(content, style, bounding_box)
def render(self, slide):
left, top, width, height = self.bounding_box
img = slide.shapes.add_picture(self.content, Inches(left), Inches(top), Inches(width), Inches(height))
# original_width, original_height = img.image.size
# # Convert dimensions from pixels to inches
# img.width = Inches(original_width * scale_factor / img.image.dpi[0])
# img.height = Inches(original_height * scale_factor / img.image.dpi[1])
self.image = img
class Table(Element):
def __init__(self, content, style, bounding_box):
super().__init__(content, style, bounding_box)
def render(self, slide):
left, top, width, height = self.bounding_box
img = slide.shapes.add_picture(self.content, Inches(left), Inches(top), Inches(width), Inches(height))
# original_width, original_height = img.image.size
# # Convert dimensions from pixels to inches
# img.width = Inches(original_width * scale_factor / img.image.dpi[0])
# img.height = Inches(original_height * scale_factor / img.image.dpi[1])
self.image = img
class Footer(Element):
def __init__(self, content, style, bounding_box, location):
super().__init__(content, style, bounding_box)
self.location = location
def render(self, slide):
left, top, width, height = self.bounding_box
textbox = slide.shapes.add_textbox(Inches(left), Inches(top), Inches(width), Inches(height))
textbox.text = self.content
self.apply_font_style(textbox)
textbox.text_frame.auto_size = True
textbox.text_frame.word_wrap = True
if self.location == 1:
textbox.text_frame.paragraphs[0].alignment = PP_ALIGN.LEFT
elif self.location == 3:
textbox.text_frame.paragraphs[0].alignment = PP_ALIGN.RIGHT
else:
textbox.text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
self.position_element(textbox)
self.textbox = textbox
class PresentationGenerator:
def __init__(self, json_payload, presentation_id):
self.json_payload = json_payload
self.presentation_id = presentation_id
if os.path.exists(os.path.join('ppts', f'{presentation_id}.pptx')):
self.presentation = Presentation(os.path.join('ppts', f'{presentation_id}.pptx'))
else:
self.presentation = Presentation()
self.insert_title_slide()
def insert_title_slide(self):
title_font = self.json_payload["slides"][0]["elements"]["title"][0]["style"]["font_name"]
title_slide = self.presentation.slides.add_slide(self.presentation.slide_layouts[0])
title_shape = title_slide.shapes.title
title_shape.text = self.json_payload['topic']
title_shape.left = Inches(0.5)
title_shape.top = Inches(2)
title_shape.width = Inches(9)
title_shape.height = Inches(1.25)
title_shape.text_frame.paragraphs[0].font.bold = True
title_shape.text_frame.paragraphs[0].font.size = Pt(48)
title_shape.text_frame.paragraphs[0].font.name = title_font
presenter = title_slide.shapes.add_textbox(Inches(3.5),Inches(3.25),Inches(3),Inches(0.75))
presenter.text = self.json_payload["presenter"]
presenter.text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
presenter.text_frame.paragraphs[0].font.italic = True
presenter.text_frame.paragraphs[0].font.size = Pt(28)
presenter.text_frame.paragraphs[0].font.name = title_font
date = title_slide.shapes.add_textbox(Inches(4),Inches(4),Inches(2),Inches(0.5))
date.text = self.json_payload["date"]
date.text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
date.text_frame.paragraphs[0].font.name = title_font
def generate_presentation(self):
slide_info = self.json_payload['slides'][-1]
slide_layout = self.presentation.slide_layouts[1]
slide = self.presentation.slides.add_slide(slide_layout)
slide.background.fill.solid()
if slide_info['bg_color']:
slide.background.fill.fore_color.rgb = RGBColor(slide_info['bg_color']['r'], slide_info['bg_color']['g'], slide_info['bg_color']['b'])
else:
slide.background.fill.fore_color.rgb = RGBColor(255, 255, 255)
for element_type, elements in slide_info['elements'].items():
for element_info in elements:
if element_type == 'figures':
element = Figure(element_info['path'], element_info['caption']['style'], (element_info['xmin'], element_info['ymin'], element_info['width'], element_info['height']), element_info['caption'])
elif element_type == 'equations':
element = Equation(element_info['path'], None, (element_info['xmin'], element_info['ymin'], element_info['width'], element_info['height']))
elif element_type == 'tables':
element = Table(element_info['path'], None, (element_info['xmin'], element_info['ymin'], element_info['width'], element_info['height']))
elif element_type == 'description':
if element_info['label'] == "enumeration":
element = Enumeration(element_info['value'], element_info['style'], (element_info['xmin'], element_info['ymin'], element_info['width'], element_info['height']), element_info['heading'])
else:
element = Description(element_info['value'], element_info['style'], (element_info['xmin'], element_info['ymin'], element_info['width'], element_info['height']))
elif element_type == 'title':
element = Title(element_info['value'], element_info['style'], (element_info['xmin'], element_info['ymin'], element_info['width'], element_info['height']))
elif element_type == 'footer':
element = Footer(element_info['value'], element_info['style'], (element_info['xmin'], element_info['ymin'], element_info['width'], element_info['height']), element_info['location'])
else:
raise ValueError(f"Unsupported element type: {element_type}")
element.render(slide)
# Clean-up empty elements
for slide in self.presentation.slides:
for shape in slide.shapes:
# print(shape.shape_type)
if shape.shape_type == MSO_SHAPE_TYPE.PLACEHOLDER:
if not shape.text_frame.text:
sp = slide.shapes._spTree
sp.remove(shape._element)
ppts_path = "ppts"
if os.path.isdir(ppts_path) == False:
os.mkdir(ppts_path)
self.presentation.save(os.path.join(ppts_path, f'{self.presentation_id}.pptx'))
os.startfile(os.path.join(ppts_path, f'{self.presentation_id}.pptx'))
def load_json_payload(file_path):
with open(file_path, 'r') as file:
return json.load(file)
def main():
# Load the JSON payload
print("\nGenerating final presentation...")
buffer_folder_path = "output"
base_topic_folder_path = "json"
json_files = [f for f in os.listdir(buffer_folder_path) if f.endswith('.json')]
for json_file in json_files:
#Load JSON file from buffer
presentation_id , _ = os.path.splitext(json_file)
json_file_path = os.path.join(buffer_folder_path, json_file)
json_payload = load_json_payload(json_file_path)
# presentation_id = json_payload["n_slides"]
#Generate Presentation from JSON file and save it
presentation_generator = PresentationGenerator(json_payload, presentation_id)
try:
presentation_generator.generate_presentation()
print(f"🟢 (1/1) Generated New presentation: {presentation_id}.")
except:
print(f"🔴 ERROR: in generating presentation")
#Move JSON file from buffer to respective topic folder
if not os.path.exists(base_topic_folder_path):
os.makedirs(base_topic_folder_path)
# destination_file_path = os.path.join(base_topic_folder_path, json_file)
# os.rename(json_file_path, destination_file_path)
# print(f"Moved {json_file} to {base_topic_folder_path}.")
# print('\n')
if __name__ == "__main__":
main()