Automatically paste images into PowerPoint materials with python + α

Introduction

There is something like pasting one image on each PowerPoint slide to make a work procedure manual. It's a transcendental simple task, but it takes a long time. It's a simple repetitive task, so I'd like to automate it using the code that was rolling on the net.

Goal implementation function

At least the features you definitely want to implement

Automatically create PowerPoint materials with one image attached to each slide.

I wish for

Slides you want to create automatically

I would like to create a character introduction slide for a certain anime that I like. The composition is Cover → Image → Character description → Image → Character description →… (Hereafter, the image and character description are alternated) It is said. The slides automatically created using the code created this time will be introduced at the end.

environment

python3.7.2 Windows10

code

ppt.py


import pptx
from pptx.util import Inches
from pptx import Presentation
from glob import glob

ppt = Presentation()
width = ppt.slide_width
height = ppt.slide_height

#Types of slides to use
title_slide_layout = ppt.slide_layouts[0] #Creating a Title Slide
bullet_slide_layout = ppt.slide_layouts[1] #Creating Title and Content
blank_slide_layout = ppt.slide_layouts[6] #Creating a blank

#Title Slide
slide = ppt.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]

title.text = "○○ Description"
subtitle.text = "kokoro pyonpyon"

#Preparing to paste the image on Blank
fnms = glob('figures/*.JPG')

tx_left = tx_top = tx_width = tx_height = Inches(1)

i = 1

for fnm in fnms:
    #Put the image on Blank
    slide_picture = ppt.slides.add_slide(blank_slide_layout)
    pic = slide_picture.shapes.add_picture(fnm, width/4,height/2, width/2, height/2)

    #Then insert a text box in the empty space
    txBox = slide_picture.shapes.add_textbox(tx_left, tx_top, tx_width*15, tx_height)
    tB = txBox.text_frame
    tB.text = "Name: "

    #Creating Title and Content
    slide_description = ppt.slides.add_slide(bullet_slide_layout)
    shapes = slide_description.shapes

    title_shape = shapes.title
    body_shape = shapes.placeholders[1]

    #Write title and text in Title and Content
    title_shape.text = 'Character description ' + str(i)
    tf = body_shape.text_frame
    tf.text = 'She works at '

    i += 1

ppt.save('figure.pptx')

Commentary

Library to import

pptx

_ A Python library that creates and updates PowerPoint files (.pptx). Typical uses include creating a customized PowerPoint document from a database or making it available for download by clicking a link on a web application. It is also possible to automatically create an engineering status report in a reportable format that summarizes information on the business management system. Sometimes I update my presentation library all at once, or automatically create one or two slides that I'm tired of working on. _

(Part of this material is translated literally.)

Well, it's enough if you know this easily. Perhaps. The brief meaning of each command is commented in the code.

Slides made using this code

pptx_title.PNG pptx_picture.PNG pptx_description.PNG

Enter the details manually for each slide. Most of it is created automatically, so it should be a lot of labor saving.

bonus

How to install the library.

$ pip install numpy

Like. Somehow I made it numpy.

Reference material

python-pptx Getting Started Paste image files in PowerPoint with Python

Recommended Posts

Automatically paste images into PowerPoint materials with python + α
Automatically format Python code into PEP8-compliant code with Emacs
Automatically translate DeepL into English with Python and Selenium
Automatically download images with scraping
Bordering images with python Part 1
[Python] GUI for inserting TeX format into PowerPoint with 2 clicks [PowerPoint]
Number recognition in images with Python
Post multiple Twitter images with python
Animate multiple still images with Python
Load gif images with Python + OpenCV
Automatically build Python documentation with Sphinx
[Python] Collect images easily with icrawler!
I tried to automatically collect images of Kanna Hashimoto with Python! !!
Working with DICOM images in Python
Creating a simple PowerPoint file with Python
INSERT into MySQL with Python [For beginners]
Automatically create Python API documentation with Sphinx
[Python] Automatically and seamlessly combine cropped images
Amplify images for machine learning with python
Capturing images with Pupil, python and OpenCV
Put protocol buffers into sqlite with python
Automatically aggregate JCG deck distribution with Python
[python, openCV] base64 Face recognition with images
[Python] Read images with OpenCV (for beginners)
Add Gaussian noise to images with python2.7
[Python] Automatically operate the browser with Selenium
Importing and exporting GeoTiff images with Python
Read text in images with python OCR
Upload images to Google Drive with Python
Automatically save images of your favorite characters from Google Image Search with Python
Divide each PowerPoint slide into a JPG file and output it with python
Make Python scripts into Windows-executable .exes with Pyinstaller
Automatically search and download YouTube videos with Python
Extract text from PowerPoint with Python! (Compatible with tables)
Generating solid color images with python | Kaggle icon
Automatically check Python scripts with GitHub + Travis-CI + pycodestyle
Convert PDFs to images in bulk with Python
Try to automatically generate Python documents with Sphinx
Continuously insert images into PowerPoint running on pywin32
[Python] Collect images with Icrawler for machine learning [1000 images]
Bulk download images from specific URLs with python
Make JSON into CSV with Python from Splunk
Materials to read when getting started with Python
I made a program to convert images into ASCII art with Python and OpenCV