Spaces:
Sleeping
Sleeping
Update signatures of the content display functions and fix empty bullet point at the beginning to text placeholders
Browse files- helpers/pptx_helper.py +85 -42
helpers/pptx_helper.py
CHANGED
|
@@ -79,10 +79,7 @@ def generate_powerpoint_presentation(
|
|
| 79 |
GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file']
|
| 80 |
)
|
| 81 |
presentation = pptx.Presentation(GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file'])
|
| 82 |
-
|
| 83 |
-
slide_width_inch = EMU_TO_INCH_SCALING_FACTOR * presentation.slide_width
|
| 84 |
-
slide_height_inch = EMU_TO_INCH_SCALING_FACTOR * presentation.slide_height
|
| 85 |
-
logger.debug('Slide width: %f, height: %f', slide_width_inch, slide_height_inch)
|
| 86 |
|
| 87 |
# The title slide
|
| 88 |
title_slide_layout = presentation.slide_layouts[0]
|
|
@@ -97,33 +94,30 @@ def generate_powerpoint_presentation(
|
|
| 97 |
subtitle.text = 'by Myself and SlideDeck AI :)'
|
| 98 |
all_headers = [title.text, ]
|
| 99 |
|
| 100 |
-
# Add
|
| 101 |
for a_slide in parsed_data['slides']:
|
| 102 |
is_processing_done = _handle_double_col_layout(
|
| 103 |
presentation=presentation,
|
| 104 |
-
slide_json=a_slide
|
|
|
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
if not is_processing_done:
|
| 108 |
-
bullet_slide_layout = presentation.slide_layouts[1]
|
| 109 |
-
slide = presentation.slides.add_slide(bullet_slide_layout)
|
| 110 |
-
|
| 111 |
is_processing_done = _handle_step_by_step_process(
|
| 112 |
-
|
| 113 |
slide_json=a_slide,
|
| 114 |
slide_width_inch=slide_width_inch,
|
| 115 |
-
slide_height_inch=slide_height_inch
|
| 116 |
)
|
| 117 |
|
| 118 |
if not is_processing_done:
|
| 119 |
-
_handle_default_display(
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
slide_height_inch=slide_height_inch,
|
| 126 |
-
)
|
| 127 |
|
| 128 |
# The thank-you slide
|
| 129 |
last_slide_layout = presentation.slide_layouts[0]
|
|
@@ -158,16 +152,23 @@ def get_flat_list_of_contents(items: list, level: int) -> List[Tuple]:
|
|
| 158 |
|
| 159 |
|
| 160 |
def _handle_default_display(
|
| 161 |
-
|
| 162 |
slide_json: dict,
|
|
|
|
|
|
|
| 163 |
):
|
| 164 |
"""
|
| 165 |
Display a list of text in a slide.
|
| 166 |
|
| 167 |
-
:param
|
| 168 |
:param slide_json: The content of the slide as JSON data.
|
|
|
|
|
|
|
| 169 |
"""
|
| 170 |
|
|
|
|
|
|
|
|
|
|
| 171 |
shapes = slide.shapes
|
| 172 |
title_shape = shapes.title
|
| 173 |
body_shape = shapes.placeholders[1]
|
|
@@ -180,21 +181,35 @@ def _handle_default_display(
|
|
| 180 |
|
| 181 |
flat_items_list = get_flat_list_of_contents(slide_json['bullet_points'], level=0)
|
| 182 |
|
| 183 |
-
for an_item in flat_items_list:
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
|
| 189 |
def _handle_double_col_layout(
|
| 190 |
presentation: pptx.Presentation(),
|
| 191 |
-
slide_json: dict
|
|
|
|
|
|
|
| 192 |
) -> bool:
|
| 193 |
"""
|
| 194 |
Add a slide with a double column layout for comparison.
|
| 195 |
|
| 196 |
:param presentation: The presentation object.
|
| 197 |
:param slide_json: The content of the slide as JSON data.
|
|
|
|
|
|
|
| 198 |
:return: True if double col layout has been added; False otherwise.
|
| 199 |
"""
|
| 200 |
|
|
@@ -210,9 +225,7 @@ def _handle_double_col_layout(
|
|
| 210 |
shapes = slide.shapes
|
| 211 |
title_placeholder = shapes.title
|
| 212 |
title_placeholder.text = remove_slide_number_from_heading(slide_json['heading'])
|
| 213 |
-
|
| 214 |
-
print(placeholder.placeholder_format.idx, placeholder.name)
|
| 215 |
-
# text_frame = body_shape.text_frame
|
| 216 |
left_heading, right_heading = shapes.placeholders[1], shapes.placeholders[3]
|
| 217 |
left_col, right_col = shapes.placeholders[2], shapes.placeholders[4]
|
| 218 |
left_col_frame, right_col_frame = left_col.text_frame, right_col.text_frame
|
|
@@ -224,10 +237,13 @@ def _handle_double_col_layout(
|
|
| 224 |
double_col_content[0]['bullet_points'], level=0
|
| 225 |
)
|
| 226 |
|
| 227 |
-
for an_item in flat_items_list:
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
if 'heading' in double_col_content[1]:
|
| 233 |
right_heading.text = double_col_content[1]['heading']
|
|
@@ -236,10 +252,20 @@ def _handle_double_col_layout(
|
|
| 236 |
double_col_content[1]['bullet_points'], level=0
|
| 237 |
)
|
| 238 |
|
| 239 |
-
for an_item in flat_items_list:
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
return True
|
| 245 |
|
|
@@ -247,7 +273,7 @@ def _handle_double_col_layout(
|
|
| 247 |
|
| 248 |
|
| 249 |
def _handle_step_by_step_process(
|
| 250 |
-
|
| 251 |
slide_json: dict,
|
| 252 |
slide_width_inch: float,
|
| 253 |
slide_height_inch: float
|
|
@@ -255,7 +281,7 @@ def _handle_step_by_step_process(
|
|
| 255 |
"""
|
| 256 |
Add shapes to display a step-by-step process in the slide, if available.
|
| 257 |
|
| 258 |
-
:param
|
| 259 |
:param slide_json: The content of the slide as JSON data.
|
| 260 |
:param slide_width_inch: The width of the slide in inches.
|
| 261 |
:param slide_height_inch: The height of the slide in inches.
|
|
@@ -296,6 +322,8 @@ def _handle_step_by_step_process(
|
|
| 296 |
):
|
| 297 |
return False
|
| 298 |
|
|
|
|
|
|
|
| 299 |
shapes = slide.shapes
|
| 300 |
shapes.title.text = remove_slide_number_from_heading(slide_json['heading'])
|
| 301 |
|
|
@@ -345,7 +373,7 @@ def _handle_step_by_step_process(
|
|
| 345 |
|
| 346 |
|
| 347 |
def _handle_key_message(
|
| 348 |
-
|
| 349 |
slide_json: dict,
|
| 350 |
slide_width_inch: float,
|
| 351 |
slide_height_inch: float
|
|
@@ -353,7 +381,7 @@ def _handle_key_message(
|
|
| 353 |
"""
|
| 354 |
Add a shape to display the key message in the slide, if available.
|
| 355 |
|
| 356 |
-
:param
|
| 357 |
:param slide_json: The content of the slide as JSON data.
|
| 358 |
:param slide_width_inch: The width of the slide in inches.
|
| 359 |
:param slide_height_inch: The height of the slide in inches.
|
|
@@ -364,7 +392,7 @@ def _handle_key_message(
|
|
| 364 |
width = pptx.util.Inches(slide_width_inch / 2.3)
|
| 365 |
top = pptx.util.Inches(slide_height_inch - height.inches - 0.1)
|
| 366 |
left = pptx.util.Inches((slide_width_inch - width.inches) / 2)
|
| 367 |
-
shape =
|
| 368 |
MSO_AUTO_SHAPE_TYPE.ROUNDED_RECTANGLE,
|
| 369 |
left=left,
|
| 370 |
top=top,
|
|
@@ -374,6 +402,21 @@ def _handle_key_message(
|
|
| 374 |
shape.text = slide_json['key_message']
|
| 375 |
|
| 376 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
if __name__ == '__main__':
|
| 378 |
_JSON_DATA = '''
|
| 379 |
{
|
|
|
|
| 79 |
GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file']
|
| 80 |
)
|
| 81 |
presentation = pptx.Presentation(GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file'])
|
| 82 |
+
slide_width_inch, slide_height_inch = _get_slide_width_height_inches(presentation)
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# The title slide
|
| 85 |
title_slide_layout = presentation.slide_layouts[0]
|
|
|
|
| 94 |
subtitle.text = 'by Myself and SlideDeck AI :)'
|
| 95 |
all_headers = [title.text, ]
|
| 96 |
|
| 97 |
+
# Add content in a loop
|
| 98 |
for a_slide in parsed_data['slides']:
|
| 99 |
is_processing_done = _handle_double_col_layout(
|
| 100 |
presentation=presentation,
|
| 101 |
+
slide_json=a_slide,
|
| 102 |
+
slide_width_inch=slide_width_inch,
|
| 103 |
+
slide_height_inch=slide_height_inch
|
| 104 |
)
|
| 105 |
|
| 106 |
if not is_processing_done:
|
|
|
|
|
|
|
|
|
|
| 107 |
is_processing_done = _handle_step_by_step_process(
|
| 108 |
+
presentation=presentation,
|
| 109 |
slide_json=a_slide,
|
| 110 |
slide_width_inch=slide_width_inch,
|
| 111 |
+
slide_height_inch=slide_height_inch
|
| 112 |
)
|
| 113 |
|
| 114 |
if not is_processing_done:
|
| 115 |
+
_handle_default_display(
|
| 116 |
+
presentation=presentation,
|
| 117 |
+
slide_json=a_slide,
|
| 118 |
+
slide_width_inch=slide_width_inch,
|
| 119 |
+
slide_height_inch=slide_height_inch
|
| 120 |
+
)
|
|
|
|
|
|
|
| 121 |
|
| 122 |
# The thank-you slide
|
| 123 |
last_slide_layout = presentation.slide_layouts[0]
|
|
|
|
| 152 |
|
| 153 |
|
| 154 |
def _handle_default_display(
|
| 155 |
+
presentation: pptx.Presentation,
|
| 156 |
slide_json: dict,
|
| 157 |
+
slide_width_inch: float,
|
| 158 |
+
slide_height_inch: float
|
| 159 |
):
|
| 160 |
"""
|
| 161 |
Display a list of text in a slide.
|
| 162 |
|
| 163 |
+
:param presentation: The presentation object.
|
| 164 |
:param slide_json: The content of the slide as JSON data.
|
| 165 |
+
:param slide_width_inch: The width of the slide in inches.
|
| 166 |
+
:param slide_height_inch: The height of the slide in inches.
|
| 167 |
"""
|
| 168 |
|
| 169 |
+
bullet_slide_layout = presentation.slide_layouts[1]
|
| 170 |
+
slide = presentation.slides.add_slide(bullet_slide_layout)
|
| 171 |
+
|
| 172 |
shapes = slide.shapes
|
| 173 |
title_shape = shapes.title
|
| 174 |
body_shape = shapes.placeholders[1]
|
|
|
|
| 181 |
|
| 182 |
flat_items_list = get_flat_list_of_contents(slide_json['bullet_points'], level=0)
|
| 183 |
|
| 184 |
+
for idx, an_item in enumerate(flat_items_list):
|
| 185 |
+
if idx == 0:
|
| 186 |
+
text_frame.text = an_item[0].removeprefix(STEP_BY_STEP_PROCESS_MARKER)
|
| 187 |
+
else:
|
| 188 |
+
paragraph = text_frame.add_paragraph()
|
| 189 |
+
paragraph.text = an_item[0].removeprefix(STEP_BY_STEP_PROCESS_MARKER)
|
| 190 |
+
paragraph.level = an_item[1]
|
| 191 |
+
|
| 192 |
+
_handle_key_message(
|
| 193 |
+
the_slide=slide,
|
| 194 |
+
slide_json=slide_json,
|
| 195 |
+
slide_height_inch=slide_height_inch,
|
| 196 |
+
slide_width_inch=slide_width_inch
|
| 197 |
+
)
|
| 198 |
|
| 199 |
|
| 200 |
def _handle_double_col_layout(
|
| 201 |
presentation: pptx.Presentation(),
|
| 202 |
+
slide_json: dict,
|
| 203 |
+
slide_width_inch: float,
|
| 204 |
+
slide_height_inch: float
|
| 205 |
) -> bool:
|
| 206 |
"""
|
| 207 |
Add a slide with a double column layout for comparison.
|
| 208 |
|
| 209 |
:param presentation: The presentation object.
|
| 210 |
:param slide_json: The content of the slide as JSON data.
|
| 211 |
+
:param slide_width_inch: The width of the slide in inches.
|
| 212 |
+
:param slide_height_inch: The height of the slide in inches.
|
| 213 |
:return: True if double col layout has been added; False otherwise.
|
| 214 |
"""
|
| 215 |
|
|
|
|
| 225 |
shapes = slide.shapes
|
| 226 |
title_placeholder = shapes.title
|
| 227 |
title_placeholder.text = remove_slide_number_from_heading(slide_json['heading'])
|
| 228 |
+
|
|
|
|
|
|
|
| 229 |
left_heading, right_heading = shapes.placeholders[1], shapes.placeholders[3]
|
| 230 |
left_col, right_col = shapes.placeholders[2], shapes.placeholders[4]
|
| 231 |
left_col_frame, right_col_frame = left_col.text_frame, right_col.text_frame
|
|
|
|
| 237 |
double_col_content[0]['bullet_points'], level=0
|
| 238 |
)
|
| 239 |
|
| 240 |
+
for idx, an_item in enumerate(flat_items_list):
|
| 241 |
+
if idx == 0:
|
| 242 |
+
left_col_frame.text = an_item[0].removeprefix(STEP_BY_STEP_PROCESS_MARKER)
|
| 243 |
+
else:
|
| 244 |
+
paragraph = left_col_frame.add_paragraph()
|
| 245 |
+
paragraph.text = an_item[0].removeprefix(STEP_BY_STEP_PROCESS_MARKER)
|
| 246 |
+
paragraph.level = an_item[1]
|
| 247 |
|
| 248 |
if 'heading' in double_col_content[1]:
|
| 249 |
right_heading.text = double_col_content[1]['heading']
|
|
|
|
| 252 |
double_col_content[1]['bullet_points'], level=0
|
| 253 |
)
|
| 254 |
|
| 255 |
+
for idx, an_item in enumerate(flat_items_list):
|
| 256 |
+
if idx == 0:
|
| 257 |
+
right_col_frame.text = an_item[0].removeprefix(STEP_BY_STEP_PROCESS_MARKER)
|
| 258 |
+
else:
|
| 259 |
+
paragraph = right_col_frame.add_paragraph()
|
| 260 |
+
paragraph.text = an_item[0].removeprefix(STEP_BY_STEP_PROCESS_MARKER)
|
| 261 |
+
paragraph.level = an_item[1]
|
| 262 |
+
|
| 263 |
+
_handle_key_message(
|
| 264 |
+
the_slide=slide,
|
| 265 |
+
slide_json=slide_json,
|
| 266 |
+
slide_height_inch=slide_height_inch,
|
| 267 |
+
slide_width_inch=slide_width_inch
|
| 268 |
+
)
|
| 269 |
|
| 270 |
return True
|
| 271 |
|
|
|
|
| 273 |
|
| 274 |
|
| 275 |
def _handle_step_by_step_process(
|
| 276 |
+
presentation: pptx.Presentation,
|
| 277 |
slide_json: dict,
|
| 278 |
slide_width_inch: float,
|
| 279 |
slide_height_inch: float
|
|
|
|
| 281 |
"""
|
| 282 |
Add shapes to display a step-by-step process in the slide, if available.
|
| 283 |
|
| 284 |
+
:param presentation: The presentation object.
|
| 285 |
:param slide_json: The content of the slide as JSON data.
|
| 286 |
:param slide_width_inch: The width of the slide in inches.
|
| 287 |
:param slide_height_inch: The height of the slide in inches.
|
|
|
|
| 322 |
):
|
| 323 |
return False
|
| 324 |
|
| 325 |
+
bullet_slide_layout = presentation.slide_layouts[1]
|
| 326 |
+
slide = presentation.slides.add_slide(bullet_slide_layout)
|
| 327 |
shapes = slide.shapes
|
| 328 |
shapes.title.text = remove_slide_number_from_heading(slide_json['heading'])
|
| 329 |
|
|
|
|
| 373 |
|
| 374 |
|
| 375 |
def _handle_key_message(
|
| 376 |
+
the_slide: pptx.slide.Slide,
|
| 377 |
slide_json: dict,
|
| 378 |
slide_width_inch: float,
|
| 379 |
slide_height_inch: float
|
|
|
|
| 381 |
"""
|
| 382 |
Add a shape to display the key message in the slide, if available.
|
| 383 |
|
| 384 |
+
:param the_slide: The slide to be processed.
|
| 385 |
:param slide_json: The content of the slide as JSON data.
|
| 386 |
:param slide_width_inch: The width of the slide in inches.
|
| 387 |
:param slide_height_inch: The height of the slide in inches.
|
|
|
|
| 392 |
width = pptx.util.Inches(slide_width_inch / 2.3)
|
| 393 |
top = pptx.util.Inches(slide_height_inch - height.inches - 0.1)
|
| 394 |
left = pptx.util.Inches((slide_width_inch - width.inches) / 2)
|
| 395 |
+
shape = the_slide.shapes.add_shape(
|
| 396 |
MSO_AUTO_SHAPE_TYPE.ROUNDED_RECTANGLE,
|
| 397 |
left=left,
|
| 398 |
top=top,
|
|
|
|
| 402 |
shape.text = slide_json['key_message']
|
| 403 |
|
| 404 |
|
| 405 |
+
def _get_slide_width_height_inches(presentation: pptx.Presentation) -> Tuple[float, float]:
|
| 406 |
+
"""
|
| 407 |
+
Get the dimensions of a slide in inches.
|
| 408 |
+
|
| 409 |
+
:param presentation: The presentation object.
|
| 410 |
+
:return: The width and the height.
|
| 411 |
+
"""
|
| 412 |
+
|
| 413 |
+
slide_width_inch = EMU_TO_INCH_SCALING_FACTOR * presentation.slide_width
|
| 414 |
+
slide_height_inch = EMU_TO_INCH_SCALING_FACTOR * presentation.slide_height
|
| 415 |
+
# logger.debug('Slide width: %f, height: %f', slide_width_inch, slide_height_inch)
|
| 416 |
+
|
| 417 |
+
return slide_width_inch, slide_height_inch
|
| 418 |
+
|
| 419 |
+
|
| 420 |
if __name__ == '__main__':
|
| 421 |
_JSON_DATA = '''
|
| 422 |
{
|