Paste a large number of image files into PowerPoint [python-pptx]

Overview

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.

environment

macOS Catalina 10.15.7 python3.8.0 Microsoft PowerPoint for Mac, version 16.43 (20110804)

Installation

pip install python-pptx
pip install Pillow

policy

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.

code

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)

in conclusion

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

Paste a large number of image files into PowerPoint [python-pptx]
Organize a large number of files into folders
Convert a large number of PDF files to text files using pdfminer
Connect a large number of videos together!
ETL processing for a large number of GTFS Realtime files (Python edition)
Upload a large number of images to Wordpress
One-liner to create a large number of test files at once on Linux
Accelerate a large number of simple queries with MySQL
[Python] Randomly generate a large number of English names
Consolidate a large number of CSV files in folders with python (data without header)
Run a limited number of image presentation programs on PsychoPy
Make a given number of seconds into hours, minutes and seconds
Scrapy-Redis is recommended for crawling a large number of domains
[Python] Easy reading of serial number image files with OpenCV
Executing a large number of Python3 Executor.submit may consume a lot of memory
TensorFlow To learn from a large number of images ... ~ (almost) solution ~
A memorandum of files under conf.d
Use shutil to delete all folders with a small number of files
TensorFlow To learn from a large number of images ... (Unsolved problem) → 12/18 Solved
Maximum average number of daily visitors (large)