I'm trying to insert a picture that is re-sized to fit the dimensions of the picture placeholder from a template using python-pptx. I don't believe the API has direct access to this from what I can find out in the docs. Is there any suggestion of how I might be able to do this, using the library or other?
I have a running code that will insert a series of images into a set of template slides to automatically create a report using Powerpoint.
Here is the function that is doing the majority of the work relevant. Other parts of the app are creating the Presentation and inserting a slide etc.
def insert_images(slide, slide_num, images_path, image_df):
"""
Insert images into a slide.
:param slide: = slide object from Presentation class
:param slide_num: the template slide number for formatting
:param images_path: the directory to the folder with all the images
:param image_df: Pandas data frame regarding information of each image in images_path
:return: None
"""
placeholders = get_image_placeholders(slide)
#print(placeholders)
image_pool = image_df[image_df['slide_num'] == slide_num]
try:
assert len(placeholders) == len(image_pool.index)
except AssertionError:
print('Length of placeholders in slide does not match image naming.')
i = 0
for idx, image in image_pool.iterrows():
#print(image)
image_path = os.path.join(images_path, image.path)
pic = slide.placeholders[placeholders[i]].insert_picture(image_path)
#print(image.path)
# TODO: Add resize - get dimensions of pic placeholder
line = pic.line
print(image['view'])
if image['view'] == 'red':
line.color.rgb = RGBColor(255, 0, 0)
elif image['view'] == 'green':
line.color.rgb = RGBColor(0, 255, 0)
elif image['view'] == 'blue':
line.color.rgb = RGBColor(0, 0, 255)
else:
line.color.rgb = RGBColor(0, 0, 0)
line.width = Pt(2.25)
i+=1
The issue is that when I insert a picture into the picture placeholder, the image is cropped, not re-sized. I don't want the user to know the dimensions to hard code into my script. If the image used is relatively large it can crop a very large portion and just not be usable.