barunsaha commited on
Commit
ace429a
·
1 Parent(s): f64ef46

Add error handling to picture XML changes

Browse files
Files changed (1) hide show
  1. helpers/pptx_helper.py +45 -15
helpers/pptx_helper.py CHANGED
@@ -469,7 +469,6 @@ def _handle_display_image__in_background(
469
  body_shape = slide.shapes.placeholders[placeholders[0][0]]
470
 
471
  title_shape.text = remove_slide_number_from_heading(slide_json['heading'])
472
-
473
  flat_items_list = get_flat_list_of_contents(slide_json['bullet_points'], level=0)
474
  add_bulleted_items(body_shape.text_frame, flat_items_list)
475
 
@@ -490,16 +489,38 @@ def _handle_display_image__in_background(
490
  width=pptx.util.Inches(slide_width_inch),
491
  )
492
 
493
- # Print the XML for debugging
494
- blip = picture._element.xpath('.//a:blip')[0]
495
- # Add transparency to the image through the blip properties
496
- alpha_mod = blip.makeelement(
497
- '{http://schemas.openxmlformats.org/drawingml/2006/main}alphaModFix'
498
- )
499
- alpha_mod.set('amt', '50000') # 50% opacity
500
- blip.append(alpha_mod)
501
- logger.debug('Blip element after: %s', blip.xml)
502
- picture._element.xpath('.//a:blip')[0].append(alpha_mod)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503
 
504
  _add_text_at_bottom(
505
  slide=slide,
@@ -510,14 +531,23 @@ def _handle_display_image__in_background(
510
  )
511
 
512
  # Move picture to background
513
- # https://github.com/scanny/python-pptx/issues/49#issuecomment-137172836
514
- slide.shapes._spTree.remove(picture._element)
515
- slide.shapes._spTree.insert(2, picture._element)
 
 
 
 
 
 
 
 
516
  except Exception as ex:
517
  logger.error(
518
- '*** Error occurred while running adding image to the slide background: %s',
519
  str(ex)
520
  )
 
521
 
522
  return True
523
 
 
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,
 
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