barunsaha commited on
Commit
18733f2
·
unverified ·
2 Parent(s): 8dd1b3a ace429a

Merge pull request #138 from barun-saha/visual

Browse files
Files changed (1) hide show
  1. helpers/pptx_helper.py +54 -9
helpers/pptx_helper.py CHANGED
@@ -213,6 +213,10 @@ def generate_powerpoint_presentation(
213
 
214
  except Exception:
215
  # In case of any unforeseen error, try to salvage what is available
 
 
 
 
216
  continue
217
 
218
  # The thank-you slide
@@ -356,7 +360,7 @@ def _handle_default_display(
356
 
357
 
358
  def _handle_display_image__in_foreground(
359
- presentation: pptx.Presentation(),
360
  slide_json: dict,
361
  slide_width_inch: float,
362
  slide_height_inch: float
@@ -434,7 +438,7 @@ def _handle_display_image__in_foreground(
434
 
435
 
436
  def _handle_display_image__in_background(
437
- presentation: pptx.Presentation(),
438
  slide_json: dict,
439
  slide_width_inch: float,
440
  slide_height_inch: float
@@ -465,7 +469,6 @@ def _handle_display_image__in_background(
465
  body_shape = slide.shapes.placeholders[placeholders[0][0]]
466
 
467
  title_shape.text = remove_slide_number_from_heading(slide_json['heading'])
468
-
469
  flat_items_list = get_flat_list_of_contents(slide_json['bullet_points'], level=0)
470
  add_bulleted_items(body_shape.text_frame, flat_items_list)
471
 
@@ -486,6 +489,39 @@ def _handle_display_image__in_background(
486
  width=pptx.util.Inches(slide_width_inch),
487
  )
488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
  _add_text_at_bottom(
490
  slide=slide,
491
  slide_width_inch=slide_width_inch,
@@ -495,20 +531,29 @@ def _handle_display_image__in_background(
495
  )
496
 
497
  # Move picture to background
498
- # https://github.com/scanny/python-pptx/issues/49#issuecomment-137172836
499
- slide.shapes._spTree.remove(picture._element)
500
- slide.shapes._spTree.insert(2, picture._element)
 
 
 
 
 
 
 
 
501
  except Exception as ex:
502
  logger.error(
503
- '*** Error occurred while running adding image to the slide background: %s',
504
  str(ex)
505
  )
 
506
 
507
  return True
508
 
509
 
510
  def _handle_icons_ideas(
511
- presentation: pptx.Presentation(),
512
  slide_json: dict,
513
  slide_width_inch: float,
514
  slide_height_inch: float
@@ -656,7 +701,7 @@ def _add_text_at_bottom(
656
 
657
 
658
  def _handle_double_col_layout(
659
- presentation: pptx.Presentation(),
660
  slide_json: dict,
661
  slide_width_inch: float,
662
  slide_height_inch: float
 
213
 
214
  except Exception:
215
  # In case of any unforeseen error, try to salvage what is available
216
+ logger.error(
217
+ 'An error occurred while processing a slide...continuing with the next one',
218
+ exc_info=True
219
+ )
220
  continue
221
 
222
  # The thank-you slide
 
360
 
361
 
362
  def _handle_display_image__in_foreground(
363
+ presentation: pptx.Presentation,
364
  slide_json: dict,
365
  slide_width_inch: float,
366
  slide_height_inch: float
 
438
 
439
 
440
  def _handle_display_image__in_background(
441
+ presentation: pptx.Presentation,
442
  slide_json: dict,
443
  slide_width_inch: float,
444
  slide_height_inch: float
 
469
  body_shape = slide.shapes.placeholders[placeholders[0][0]]
470
 
471
  title_shape.text = remove_slide_number_from_heading(slide_json['heading'])
 
472
  flat_items_list = get_flat_list_of_contents(slide_json['bullet_points'], level=0)
473
  add_bulleted_items(body_shape.text_frame, flat_items_list)
474
 
 
489
  width=pptx.util.Inches(slide_width_inch),
490
  )
491
 
492
+ try:
493
+ # Find all blip elements to handle potential multiple instances
494
+ blip_elements = picture._element.xpath('.//a:blip')
495
+ if not blip_elements:
496
+ logger.warning(
497
+ 'No blip element found in the picture. Transparency cannot be applied.'
498
+ )
499
+ return True
500
+
501
+ for blip in blip_elements:
502
+ # Add transparency to the image through the blip properties
503
+ alpha_mod = blip.makeelement(
504
+ '{http://schemas.openxmlformats.org/drawingml/2006/main}alphaModFix'
505
+ )
506
+ # Opacity value between 0-100000
507
+ alpha_mod.set('amt', '50000') # 50% opacity
508
+
509
+ # Check if alphaModFix already exists to avoid duplicates
510
+ existing_alpha_mod = blip.find(
511
+ '{http://schemas.openxmlformats.org/drawingml/2006/main}alphaModFix'
512
+ )
513
+ if existing_alpha_mod is not None:
514
+ blip.remove(existing_alpha_mod)
515
+
516
+ blip.append(alpha_mod)
517
+ logger.debug('Added transparency to blip element: %s', blip.xml)
518
+
519
+ except Exception as ex:
520
+ logger.error(
521
+ 'Failed to apply transparency to the image: %s. Continuing without it.',
522
+ str(ex)
523
+ )
524
+
525
  _add_text_at_bottom(
526
  slide=slide,
527
  slide_width_inch=slide_width_inch,
 
531
  )
532
 
533
  # Move picture to background
534
+ try:
535
+ slide.shapes._spTree.remove(picture._element)
536
+ slide.shapes._spTree.insert(2, picture._element)
537
+ except Exception as ex:
538
+ logger.error(
539
+ 'Failed to move image to background: %s. Image will remain in foreground.',
540
+ str(ex)
541
+ )
542
+
543
+ return True
544
+
545
  except Exception as ex:
546
  logger.error(
547
+ '*** Error occurred while adding image to the slide background: %s',
548
  str(ex)
549
  )
550
+ return True
551
 
552
  return True
553
 
554
 
555
  def _handle_icons_ideas(
556
+ presentation: pptx.Presentation,
557
  slide_json: dict,
558
  slide_width_inch: float,
559
  slide_height_inch: float
 
701
 
702
 
703
  def _handle_double_col_layout(
704
+ presentation: pptx.Presentation,
705
  slide_json: dict,
706
  slide_width_inch: float,
707
  slide_height_inch: float