Divide each PowerPoint slide into a JPG file and output it with python

Introduction

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.

Operating environment

Execution result

--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.

実行結果.jpg

How to use

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

More convenient usage

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"

AnacondaPromptのpathの調べ方.jpg AnacondaPromptのpathの調べ方2.jpg

Other miscellaneous notes

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

Execution code

# -*- 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)


in conclusion

Did not paste the sushi license. But can someone buy sushi?

Recommended Posts

Divide each PowerPoint slide into a JPG file and output it with python
Read json file with Python, format it, and output json
Creating a simple PowerPoint file with Python
[Python] Concatenate a List containing numbers and write it to an output file.
Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)
I made a tool in Python that right-clicks an Excel file and divides it into files for each sheet.
Associate Python Enum with a function and make it Callable
I tried to divide the file into folders with Python
Output to csv file with Python
Create a temporary file with django as a zip file and return it
[Python] How to scrape a local html file and output it as CSV using Beautiful Soup
2. Make a decision tree from 0 with Python and understand it (2. Python program basics)
Read and format a csv file mixed with comma tabs with Python pandas
Deploy a Python app on Google App Engine and integrate it with GitHub
A memo with Python2.7 and Python3 on CentOS
I ran GhostScript with python, split the PDF into pages, and converted it to a JPEG image.
A python program that resizes a video and turns it into an image
Read CSV file with Python and convert it to DataFrame as it is
Edit Slide (PowerPoint for Google) with Python (Low-cost RPA case with Google API and Python)
Make a decision tree from 0 with Python and understand it (4. Data structure)
Create a decision tree from 0 with Python and understand it (5. Information Entropy)
I made a configuration file with Python
I generated a lot of images like Google Calendar favicon with Python and incorporated it into Vue's project
I want to write an element to a file with numpy and check it.
Creating a temperature control system with Raspberry Pi and ESP32 (3) Recipient Python file
How to read a CSV file with Python 2/3
[Python] I introduced Word2Vec and played with it.
Building a python environment with virtualenv and direnv
Read JSON with Python and output as CSV
Output python log to both console and file
Launch a web server with Python and Flask
Let's write a Python program and run it
Create a Photoshop format file (.psd) with python
Read line by line from a file with Python
I want to write to a file with Python
Open a file dialog with a python GUI (tkinter.filedialog)
Create a cylinder with open3d + STL file output
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
Quickly create a Python data analysis dashboard with Streamlit and deploy it to AWS
[Python] A notebook that translates and downloads the ipynb file on GitHub into Japanese.
Until you create a machine learning environment with Python on Windows 7 and run it
It is easy to execute SQL with Python and output the result in Excel
Automatically paste images into PowerPoint materials with python + α
Procedure to load MNIST with python and output to png
Make each PowerPoint page an image file in Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Install selenium on Mac and try it with python
Build a python environment for each directory with pyenv-virtualenv
Let's make a simple game with Python 3 and iPhone
I made a LINE BOT with Python and Heroku
Build a python virtual environment with virtualenv and virtualenvwrapper
Get mail from Gmail and label it with Python3
[Python-pptx] Output PowerPoint font information to csv with python
Run a Python file with relative import in PyCharm
[Python] Create a Tkinter program distribution file with cx_Freeze
Output a binary dump in binary and revert to a binary file
Using a Python program with fluentd's exec_filter Output Plugin
Automatically translate DeepL into English with Python and Selenium
Create a 2d CAD file ".dxf" with python [ezdxf]
[Python] Start a batch file from Python and pass variables.
Using a python program with fluentd's exec Output Plugin