How To Convert PowerPoint PPT into JPGs and Combine Them As a Single File with Python?

Yanwei Liu
2 min readFeb 2, 2020
A example of this project

I read an article from WeChat公眾號, it claims that the code can convert PPT into images, and combine them as a single png file. But after I just copy and paste the python code, nothing happened but error occured.

In this article, I try to modify the code and finally it actually works.

Install the requirments package

pip install pypiwin32
pip install Pillow

Let’s coding:

import os
import win32com.client
from PIL import Image
#Input the absolute path to the ppt file
ppt_path = input('請輸入PPT檔案絕對路徑:')
#If you want to generate Singe long image
long_sign = input('是否產生長圖(Y/N):')
# Create a ppt2jpg function
def ppt2jpg():
output_path = ppt_path #output path is the same as ppt path
ppt_app = win32com.client.Dispatch('PowerPoint.Application')
ppt = ppt_app.Presentations.Open(ppt_path) #start PowerPoint
ppt.SaveAs(output_path, 17) #17 is the number to save as jpg file type
ppt_app.Quit() # close PowerPoint
if 'Y' == long_sign.upper():
generate_long_image(output_path) # generate long image
# A function to generate long imagedef generate_long_image(output_path):
picture_path = output_path[:output_path.rfind('.')]
last_dir = os.path.dirname(picture_path) #
# Get the images
ims = [Image.open(os.path.join(picture_path, fn)) for fn in os.listdir(picture_path)]
# get the first image size
width, height = ims[0].size
# create new blank image(using image's width and height*how many images do we have as its size.)
long_canvas = Image.new(ims[0].mode, (width, height * len(ims)))
# combine image
for i, image in enumerate(ims):
long_canvas.paste(image, box=(0, i * height))
# save image
long_canvas.save(os.path.join(last_dir, 'long-image.png'))
print('長圖保存成功!') # image saved successfully
ppt2jpg() #call the function

--

--