@@ -43,37 +43,37 @@ def replace_picture(self, label: str, filename: _Pathlike, *, do_not_scale_up: b
4343 """
4444 shapes_to_replace = self ._find_shapes (label )
4545 if not shapes_to_replace :
46- return
46+ raise ValueError ( f"The label ' { label } ' can't be found in the template." )
4747 if isinstance (filename , str ):
4848 filename = pathlib .Path (filename )
4949 if not filename .is_file ():
5050 raise FileNotFoundError (f"The file does not exist: { filename } " )
51- img_file = open (filename , "rb" )
52- old_shape : BaseShape
53- for old_shape in shapes_to_replace :
54- slide_shapes = old_shape ._parent
55- img_shape = slide_shapes .add_picture (
56- image_file = img_file ,
57- left = old_shape .left ,
58- top = old_shape .top ,
59- )
60- # Scaling the image if `do_not_scale == False`:
61- if img_shape .height <= old_shape .height and img_shape .width <= old_shape .width and not do_not_scale_up :
62- old_aspect_ratio = old_shape .width / old_shape .height
63- new_aspect_ratio = img_shape .width / img_shape .height
64- if old_aspect_ratio >= new_aspect_ratio :
65- img_shape .width = old_shape .width
66- img_shape .height = int (img_shape .width / new_aspect_ratio )
67- else :
68- img_shape .height = old_shape .height
69- img_shape .width = int (img_shape .height * new_aspect_ratio )
70- # Centering the image at the extent of the placeholder:
71- img_shape .top += int ((old_shape .height - img_shape .height ) / 2 )
72- img_shape .left += int ((old_shape .width - img_shape .width ) / 2 )
73- del slide_shapes [slide_shapes .index (old_shape )]
74- # Removing shapes is performed at the lxml level.
75- # The `element` attribute contains an instance of `lxml.etree._Element`.
76- slide_shapes .element .remove (old_shape .element )
51+ with open (filename , "rb" ) as img_file :
52+ old_shape : BaseShape
53+ for old_shape in shapes_to_replace :
54+ slide_shapes = old_shape ._parent
55+ img_shape = slide_shapes .add_picture (
56+ image_file = img_file ,
57+ left = old_shape .left ,
58+ top = old_shape .top ,
59+ )
60+ # Scaling the image if `do_not_scale == False`:
61+ if img_shape .height <= old_shape .height and img_shape .width <= old_shape .width and not do_not_scale_up :
62+ old_aspect_ratio = old_shape .width / old_shape .height
63+ new_aspect_ratio = img_shape .width / img_shape .height
64+ if old_aspect_ratio >= new_aspect_ratio :
65+ img_shape .width = old_shape .width
66+ img_shape .height = int (img_shape .width / new_aspect_ratio )
67+ else :
68+ img_shape .height = old_shape .height
69+ img_shape .width = int (img_shape .height * new_aspect_ratio )
70+ # Centering the image at the extent of the placeholder:
71+ img_shape .top += int ((old_shape .height - img_shape .height ) / 2 )
72+ img_shape .left += int ((old_shape .width - img_shape .width ) / 2 )
73+ del slide_shapes [slide_shapes .index (old_shape )]
74+ # Removing shapes is performed at the lxml level.
75+ # The `element` attribute contains an instance of `lxml.etree._Element`.
76+ slide_shapes .element .remove (old_shape .element )
7777
7878 def replace_table (self , label : str , data ) -> None :
7979 """Replaces rectangle placeholders on one or many slides.
0 commit comments