Python — Automating Image Transfer to PowerPoint

EeLianChong
4 min readNov 10, 2024

--

***

This is another short, overly simplified geeky post.

***

Some people think of automation as any codes that runs in python. But they tend to forget that if they are supposed to painstakingly copy paste and run the codes monthly, that’s not even half-automation. 😅😅😅

This use case [image transfer to powerpoint] is probably unique to very few organizations where compilation of images or charts is required from various teams. Back in organization Y more than 10 years ago, we used to run automated macros in excel + powerpoint to get site images updated and compiled magically monthly, courtesy of the very lazy yours truly. 🤣

***

Anyway, moving on to 2024, i will be sharing two super simple methods used to compile and transfer images files to a powerpoint deck.

In these examples, pokemon images will be used to replace a deck with Malaysian flags. Do drop me a note if you need the sample files.

***

Option One

One may opt for Option 1 to manually run like this, as per how the report analysts in organization X (circa 2024) does. It takes a ridiculously long amount of time (XX days) to update and run the codes monthly. 😅

For 200 images, that’s 200 x 8 lines of codes haha. Imagine when it comes to requirement to

  • [1] insert an additional slide in between slide 13 hahahaha; and changing the subsequent hardcoded slide numbers from slide 13 onwards for each images hahahaha. 🤣
  • [2] update the image sizes? 🫢
# import libraries
import os as os
import numpy as np
import pandas as pd

from pptx import Presentation
from pptx.util import Inches
from pptx.util import Cm

# set directory for image and ppt slide
dir_path='C:\\Users\\EeLian\\Desktop\\Work_\\ImageTransfer\\Images\\'
slide_path = 'C:\\Users\\EeLian\\Desktop\\Work_\\ImageTransfer\\Slides\\'

# insert the desired slide names
slide_name_pre_insertion = 'Pokemon_20240101_Before' # existing file
slide_name_post_insertion = 'Pokemon_20240101_After'
# slide_name_post_insertion refers to a non-existing file
# if exists, will be written over
# defining where and how the images should appear on powerpoint
# Method One - hardcoded method used by analysts in Organization X
def screenshots_image_transfer_method_one():

slide = prs.slides[1] #analysts updates each hardcoded parameter manually
pic = slide.shapes[0]
pic = pic._element
pic.getparent().remove(pic)
img_path = '152_grass_chikorita.png'
pic = slide.shapes.add_picture(dir_path + img_path, left=Cm(3.00), top=Cm(4.00), width=Cm(10.00), height=Cm(10.0))
slide.shapes._spTree.remove(pic._element)
slide.shapes._spTree.insert(2, pic._element)

slide = prs.slides[2]
pic = slide.shapes[0]
pic = pic._element
pic.getparent().remove(pic)
img_path = '001_grass_bulbasaur.png'
pic = slide.shapes.add_picture(dir_path + img_path, left=Cm(3.00), top=Cm(4.00), width=Cm(10.00), height=Cm(10.0))
slide.shapes._spTree.remove(pic._element)
slide.shapes._spTree.insert(2, pic._element)

slide = prs.slides[3]
pic = slide.shapes[0]
pic = pic._element
pic.getparent().remove(pic)
img_path = '080_psychic_slowbro.png'
pic = slide.shapes.add_picture(dir_path + img_path, left=Cm(3.00), top=Cm(4.00), width=Cm(10.00), height=Cm(10.0))
slide.shapes._spTree.remove(pic._element)
slide.shapes._spTree.insert(2, pic._element)

slide = prs.slides[4]
pic = slide.shapes[0]
pic = pic._element
pic.getparent().remove(pic)
img_path = '096_psychic_drowzee.png'
pic = slide.shapes.add_picture(dir_path + img_path, left=Cm(3.00), top=Cm(4.00), width=Cm(10.00), height=Cm(10.0))
slide.shapes._spTree.remove(pic._element)
slide.shapes._spTree.insert(2, pic._element)

# For 200 images, that's 200 x 8 lines of codes haha. Imagine that.
# transfering the images to powerpoint
prs = Presentation(slide_path + slide_name_pre_insertion + '.pptx')
screenshots_image_transfer_method_one()
prs.save(slide_path + slide_name_post_insertion + '.pptx')

***

Option Two

Alternatively, you may opt for option 2 that folks like me would do, ie

  • [1] simplify the codes to X lines using a simple loop,
  • [2] control the required image transfer using a master csv file.
  • [3] eliminate the hassle of going through multiple lines of codes and updating it every month, ie achieve some degree of automation instead of manual code-bash-run (yet claiming it’s automation).

Anyway, here’s how the parameter file looks like. The columns are self-explanatory, but if you require help with it, do drop me a note on this.

# import libraries
import os as os
import numpy as np
import pandas as pd

from pptx import Presentation
from pptx.util import Inches
from pptx.util import Cm

# set directory for image and ppt slide
dir_path='C:\\Users\\EeLian\\Desktop\\Work_\\ImageTransfer\\Images\\'
slide_path = 'C:\\Users\\EeLian\\Desktop\\Work_\\ImageTransfer\\Slides\\'

# insert the desired slide names
slide_name_pre_insertion = 'Pokemon_20240101_Before' # existing file
slide_name_post_insertion = 'Pokemon_20240101_After' # non-existing or to be written over file
# defining where and how the images should appear on powerpoint.
# Method Two - EL's recommended method, via a csv/excel definition file.

# call the relevant parameter definition file

# this file is the master list
# where we specify the parameters require to enable the image transfer to happen for all relevant slides.
# columns A,B,C are not used in the code but more for analyst references. You can have more columns if required.

Dim_Mapping_Slides_Master=pd.read_csv('Slide_Mapping_Parameters_Pokemon_20240101.csv')
Dim_Mapping_List_Master=list(Dim_Mapping_Slides_Master.iloc[:, 3:].to_records(index=False))

# codes are simplified to a non-repetitive code loop block using the above paramenter file
def screenshots_image_transfer_method_two():

for var_shape_no, var_shape_elem_no, var_slide_no, var_img_size_left \
,var_img_size_top, var_img_size_width, var_img_size_height, var_img_name \
in Dim_Mapping_List_Master:
slide = prs.slides[var_slide_no]
pic = slide.shapes[var_shape_no]
pic = pic._element
pic.getparent().remove(pic)
img_path = var_img_name+'.png'
pic = slide.shapes.add_picture(dir_path + img_path, left=Cm(var_img_size_left),
top=Cm(var_img_size_top),
width=Cm(var_img_size_width),
height=Cm(var_img_size_height))
slide.shapes._spTree.remove(pic._element)
slide.shapes._spTree.insert(var_shape_elem_no, pic._element)
# transfering the images to powerpoint
prs = Presentation(slide_path + slide_name_pre_insertion + '.pptx')
screenshots_image_transfer_method_two()
prs.save(slide_path + slide_name_post_insertion + '.pptx')

***

Conclusion

I introduced method [2] to organization X recently or perhaps not too recently 😅.

And while it took me less than 10 minutes to write the codes😄 But it took a good 6 months before analysts are willing to attempt using it 😅.

Nonetheless, there’s this satisfaction when it finally got adopted 😎

Sometimes i do wonder if some of these analysts truly lack the right skills to deliver their job well; or if these analysts are simply too afraid to embrace automation and AI out of fear of job security? 🤔

But at times it’s good to remind ourselves that 😎

“AI won’t replace radiologists, but radiologists who use AI will replace radiologists who don’t”,
- Dr. Curtis Langlotz, Stanford radiologist.

Till next time. Tschüss! 👻

***

Image sources:

***

--

--

No responses yet