Want to convert a PPT file to JPG and use it for document creation? I found a person who is working on automation (https://qiita.com/natsutan/items/2487e24fe3f31569b40d), but I thought that the following two points were insufficient.
――The slide names are slide 1 and slide 2, and if you make a lot of them, you won't know what the picture is. ――When I wrote a small picture, there was a lot of margin (to export the entire slide)
Therefore, based on the PPT slide title name, I made software that outputs each page as a JPG file.
--When you run it on Spyder, a window will appear, where you can select a PowerPoint file. --The images folder is created, and a JPG file is generated with the title name of each page. -(However, if there is a Japanese name in the execution path, it will fail.) ← python is difficult to support S-JIS.
How many margins should be allocated to the total width with Margin in the file? With whiteSpaceCutRatio, you can set what percentage to cut from the top (for title cut). Please adjust according to your own environment.
Margin = 0.1; #margin
whiteSpaceCutRatio = 0.2; #What percentage of the above should be cut? Adjust for each PPT file
If you want to generate a batch file by double-clicking, create a batch file like the one below. You can generate an image from PowerPoint by double-clicking without going through the UserInterface.
Batch file explanation
--The first line calls the anaconda prompt from the cmd prompt. How to check this path is as shown in the attached figure. The point is to put a call, otherwise it will stop at the anaconda prompt. --The second line describes pythonpath + executable file path + PowerPoint file path. When entering the PowerPoint file path here, "" is an Escape character, so it is manually converted to "\". --If you save the following file in batch file format such as test.bat, you can get PowerPoint by double-clicking. It will be converted to a JPG file. --If you don't know the path of python.exe, this page (http://rikoubou.hatenablog.com/entry/2018/11/05/174320) Please check with print (sys.path) on Spyder referring to.
call C:\Users\hogemmacho\Anaconda3\Scripts\activate.bat
C:\Users\hogemmacho\anaconda3\python.exe C:\Users\hogemmacho\Desktop\obenkyo\powerPointSlidesExportasJPG.py "C:\\Users\\hogemmacho\\Desktop\\obenkyo\\test1.pptx"
OpenCV cv2.imread cannot read Japanese file names. So, in this source, once converted to hogehogefugaFuga.jpg and processed with Opencv Reverted to the original name (reference: https://qiita.com/SKYS/items/cbde3775e2143cad7455) .. Japanese is annoying with python. And thanks to the person who wrote the article
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 13 13:49:23 2020
@author: hogemaccho
"""
import tkinter as tk
from tkinter import filedialog
import os
from comtypes import client
import cv2
import numpy as np
import shutil
import sys
# notice:
#If you use this file on a folder path that includes Japan
#Does not work properly
#
# GlobalSetting Change here
Margin = 0.1; #margin
whiteSpaceCutRatio = 0.2; #What percentage of the above should be cut? Adjust for each PPT file
# Select PowerpointFile and get Absolute path
def UIgetPPTFilePath(CurrentDirPath):
root = tk.Tk()
root.withdraw()
typ = [('PPT file','*.pptx')]
file_path = filedialog.askopenfilename(filetypes =typ, initialdir = CurrentDirPath)
return file_path
# Create "images" dir
def makeImageDir(CurrentDirPath):
ImageDirPath = CurrentDirPath+'\\images'
if not(os.path.exists(ImageDirPath)):
os.makedirs('images')
return ImageDirPath
#Open PowerPoint and save Image for SaveDirpath as JPG
def export_img(file_path,SaveDirPath):
application = client.CreateObject("Powerpoint.Application")
application.Visible = True
presentation = application.Presentations.open(file_path)
#SlideList = application.Acti
slideList = application.ActivePresentation.Slides
# Export JPG file titlename = filename
for i in range(slideList.Count):
#Initialize
title = ""
slide = slideList.Item(i+1) #Slides Start as 1
title = slide.Shapes.Title.TextFrame.TextRange.Text
JPGFullPath = SaveDirPath+'\\'+title+'.jpg'
slide.Export(JPGFullPath,'jpg')
whiteSpaceCut(JPGFullPath,whiteSpaceCutRatio)
presentation.close()
application.quit()
#Cut Jpg white space
def whiteSpaceCut(JPGFullPath,titleCutRatio):
#Copy as a countermeasure to Japanese character
tempFileName = './hogehogefugaFuga.jpg'
shutil.copy(JPGFullPath,tempFileName)
#read jpg as gray Image
ImageData = cv2.imread(tempFileName,cv2.IMREAD_GRAYSCALE)
CutImageData = cv2.imread(tempFileName,cv2.IMREAD_COLOR)
CutImageData = CutImageData[round(ImageData.shape[0]*titleCutRatio):,:,:]
#CutTitleData
GrayImage = ImageData[round(ImageData.shape[0]*titleCutRatio):,:]
#Serch White(r,g,b = 255,255,255) Area
GrayImageWidth = np.mean(GrayImage,axis = 0)
GrayImageHeight = np.mean(GrayImage,axis = 1)
GrayImageWidthStart = np.where(GrayImageWidth <255)[0][0]
GrayImageWidthEnd = np.where(GrayImageWidth <255)[0][-1]
GrayImageHeightStart = np.where(GrayImageHeight <255)[0][0]
GrayImageHeightEnd = np.where(GrayImageHeight <255)[0][-1]
#Take Margin
WidthLength = GrayImageWidthEnd - GrayImageWidthStart;
HeightLength = GrayImageHeightEnd - GrayImageHeightStart;
#Cut area
WidthStartPoint = GrayImageWidthStart - round(WidthLength*Margin);
if WidthStartPoint < 1:
WidthStartPoint = 1
WidthEndPoint = GrayImageWidthEnd + round(WidthLength*Margin);
if WidthEndPoint >= len(GrayImageWidth):
WidthEndPoint = len(GrayImageWidth)
HeightStartPoint = GrayImageHeightStart - round(HeightLength*Margin)
if HeightStartPoint < 1 :
HeightStartPoint = 1;
HeightEndPoint = GrayImageHeightEnd + round(HeightLength*Margin)
if HeightEndPoint >= len(GrayImageHeight):
HeightEndPoint = len(GrayImageHeight)
#OutputImage
OutputImageData = CutImageData[int(HeightStartPoint):int(HeightEndPoint),\
int(WidthStartPoint):int(WidthEndPoint),\
:]
cv2.imwrite(tempFileName,OutputImageData)
#rename File and delete tempprary file
shutil.copy(tempFileName,JPGFullPath)
os.remove(tempFileName)
if __name__ == '__main__':
# check command line
args = sys.argv
# Current DirPath
CurrentDirPath = os.path.dirname(__file__)
# get PPT file path using User Interface
if len(args)==1:
file_path = UIgetPPTFilePath(CurrentDirPath)
else:
file_path = args[1]
# make images dir for current folder
ImageDirPath = makeImageDir(CurrentDirPath)
# export image
export_img(file_path,ImageDirPath)
Did not paste the sushi license. But can someone buy sushi?
Recommended Posts