Skip to content

Instantly share code, notes, and snippets.

@e96031413
Created February 2, 2020 05:53
Show Gist options
  • Save e96031413/82d0f6852703d8a54f322c736d317b18 to your computer and use it in GitHub Desktop.
Save e96031413/82d0f6852703d8a54f322c736d317b18 to your computer and use it in GitHub Desktop.
A script can save ppt as jpgs and combine them into a single image
import os
import win32com.client
from PIL import Image
ppt_path = input('請輸入PPT檔案絕對路徑:')
long_sign = input('是否產生長圖(Y/N):')
def ppt2jpg():
output_path = ppt_path
ppt_app = win32com.client.Dispatch('PowerPoint.Application')
ppt = ppt_app.Presentations.Open(ppt_path) # 打開PPT軟體
ppt.SaveAs(output_path, 17) # 17是PPT轉成圖片*.jpg格式的數字
ppt_app.Quit() # 關閉PPT軟體
if 'Y' == long_sign.upper():
generate_long_image(output_path) # 合併產生長圖
def generate_long_image(output_path):
picture_path = output_path[:output_path.rfind('.')]
last_dir = os.path.dirname(picture_path) # 上一層級目錄
# 獲取單張圖片
ims = [Image.open(os.path.join(picture_path, fn)) for fn in os.listdir(picture_path)]
width, height = ims[0].size # 取第一張圖片的長寬尺寸
long_canvas = Image.new(ims[1].mode, (width, height * len(ims))) # 創建同樣寬度,n張圖片高度的空白圖片
# 拼接圖片
for i, image in enumerate(ims):
long_canvas.paste(image, box=(0, i * height))
long_canvas.save(os.path.join(last_dir, 'long-image.png')) # 保存長圖
print('長圖保存成功!')
ppt2jpg()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment