Generate a Powerpoint slide for each image in a directory in Python?

Question:

I want to make a python script that imports all images in a file to each slide. I have about 30 images and I want it to auto generate a slide for each image. So far, I thought it would be easier to put each image into an array and then make each array pop out on each slide, but I’m getting a really long error. Each image is about 1024×1024 so I centered it manually.

from pptx import Presentation #
import collections
import collections.abc
from pptx.util import Inches
import os
from PIL import Image
import glob
import cv2
import numpy as np

try:
    from collections.abc import Container
except ImportError:
    from collections import Container
collections.Container = collections.abc.Container
collections.Mapping = collections.abc.Mapping
collections.MutableMapping = collections.abc.MutableMapping
collections.Iterable = collections.abc.Iterable
collections.MutableSet = collections.abc.MutableSet
collections.Callable = collections.abc.Callable

imgarr = [] #later to be array of images
image_dir = "Z:Programming StuffImagesTesting"


#Makes the array of images into imgarr
for filename in os.listdir(image_dir):
    if filename.endswith(".jpg") or filename.endswith(".png"):
        img = Image.open(os.path.join(image_dir, filename))

        imgarr.append(img)


#Makes the slides in powerpoint
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
for numslides in range(len(imgarr)):
    slide = prs.slides.add_slide(blank_slide_layout)
    left = Inches(-.5)
    top = Inches(-1.2)
    pic = slide.shapes.add_picture(imgarr[numslides], left, top, Inches(11))



prs.save('test.pptx')
 File "c:UsersBattleShipDesktopworkPowerpointmaker.py", line 42, in <module>
    pic = slide.shapes.add_picture(imgarr[numslides], left, top, Inches(11))
  File "C:UsersBattleShipAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespptxshapesshapetree.py", line 332, in add_picture
    image_part, rId = self.part.get_or_add_image_part(image_file)
  File "C:UsersBattleShipAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespptxpartsslide.py", line 39, in get_or_add_image_part
    image_part = self._package.get_or_add_image_part(image_file)
  File "C:UsersBattleShipAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespptxpackage.py", line 36, in get_or_add_image_part
    return self._image_parts.get_or_add_image_part(image_file)
  File "C:UsersBattleShipAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespptxpackage.py", line 151, in get_or_add_image_part
    image = Image.from_file(image_file)
  File "C:UsersBattleShipAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespptxpartsimage.py", line 170, in from_file
    blob = image_file.read()
  File "C:UsersBattleShipAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesPILImage.py", line 517, in __getattr__
    raise AttributeError(name)
AttributeError: read
Asked By: Electro

||

Answers:

python-pptx expects a path to an image, and you’re passing it the image itself that you’ve already read/opened…

So changing this:

#Makes the array of images into imgarr
for filename in os.listdir(image_dir):
    if filename.endswith(".jpg") or filename.endswith(".png"):
        img = Image.open(os.path.join(image_dir, filename))

        imgarr.append(img)

to this:

#Makes the array of images into imgarr
for filename in os.listdir(image_dir):
    if filename.endswith(".jpg") or filename.endswith(".png"):
        imgarr.append(os.path.join(image_dir, filename))

should do the trick, at least as far as this error is concerned.

Answered By: ShlomiF