I wrote a python script to generate PowerPoint with image files pasted one slide at a time. When sharing a screenshot image of a PC screen taken at a web conference with other people, I thought it would be difficult to see and share a large number of image files as they are, so I made it.
macOS Catalina 10.15.7 python3.8.0 Microsoft PowerPoint for Mac, version 16.43 (20110804)
pip install python-pptx
pip install Pillow
Create a PowerPoint file by pasting the image files in the specified directory one by one on each slide. Manipulate PowerPoint objects in python using python-pptx. In order to center and paste the image, we need to get the image size, so get it with Pillow (PIL) (Reference: Center image with python-pptx). To prevent the image from sticking out of the slide, paste it to the full width when it is wider than the slide, and paste it to the full width when it is vertically long.
from pptx import Presentation
from PIL import Image
import os
#Slide size
#4:3 (default) 9144000x6858000
#16:9 12193200x6858000
SLIDE_WIDTH, SLIDE_HEIGHT = 12193200, 6858000
#X and Y coordinates of the center of the slide (upper left is the origin)
IMG_CENTER_X, IMG_CENTER_Y = SLIDE_WIDTH/2, SLIDE_HEIGHT/2
#Slide aspect ratio
SLIDE_ASPECT_RATIO = SLIDE_WIDTH / SLIDE_HEIGHT
#Output file name
OUTPUT_FILE_PATH = "test.pptx"
#Image storage directory
IMG_DIR = "./data/"
#Adds a slide to the received presentation object and returns the added slide object.
def add_slide(prs):
#Add blank slides(ID=6 is a blank slide)
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
return slide
#Paste the image in the center of the slide
def add_picture(slide, img_file):
#Get the image size and get the aspect ratio
im = Image.open(img_file)
im_width, im_height = im.size
aspect_ratio = im_width/im_height
#Branch processing according to the aspect ratio of slides and images
#If the image is horizontally long, spread it horizontally
if aspect_ratio > SLIDE_ASPECT_RATIO:
img_display_width = SLIDE_WIDTH
img_display_height = img_display_width / aspect_ratio
else: #If the image is vertically long, spread it vertically
img_display_height = SLIDE_HEIGHT
img_display_width = img_display_height * aspect_ratio
#Calculate the upper left coordinates of the image when centering
left = IMG_CENTER_X - img_display_width / 2
top = IMG_CENTER_Y - img_display_height / 2
#Add images to slides
if aspect_ratio > SLIDE_ASPECT_RATIO:
slide.shapes.add_picture(img_file, left, top, width = img_display_width)
else:
slide.shapes.add_picture(img_file, left, top, height = img_display_height)
return slide
#Definition of slide object
prs = Presentation()
#Specify slide size
prs.slide_width = SLIDE_WIDTH
prs.slide_height = SLIDE_HEIGHT
#Get the file name of the img image
img_files = os.listdir(IMG_DIR)
#Extract only file names ending with png. Change according to the extension of the image you want to paste
img_files = [name for name in img_files if name.endswith(".png ")]
#Sort in ascending order (pasted on slides in this order)
img_files.sort()#Sort in ascending order
for name in img_files:
path = IMG_DIR + name
slide = add_slide(prs)
add_picture(slide, path)
#Output pptx file
prs.save(OUTPUT_FILE_PATH)
I have a lot of opportunities to hold web conferences these days, so I think it's a useful script when I just take screenshots and share them with others later. I feel that it is also useful when I look back on myself or leave notes. I hope it helps to improve efficiency in some way.
Recommended Posts