Home > Enterprise >  Generate a Powerpoint slide for each image in a directory in Python?
Generate a Powerpoint slide for each image in a directory in Python?

Time:12-15

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 1024x1024 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 Stuff\Images\Testing"


#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:\Users\BattleShip\Desktop\work\Powerpointmaker.py", line 42, in <module>
    pic = slide.shapes.add_picture(imgarr[numslides], left, top, Inches(11))
  File "C:\Users\BattleShip\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pptx\shapes\shapetree.py", line 332, in add_picture
    image_part, rId = self.part.get_or_add_image_part(image_file)
  File "C:\Users\BattleShip\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pptx\parts\slide.py", line 39, in get_or_add_image_part
    image_part = self._package.get_or_add_image_part(image_file)
  File "C:\Users\BattleShip\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pptx\package.py", line 36, in get_or_add_image_part
    return self._image_parts.get_or_add_image_part(image_file)
  File "C:\Users\BattleShip\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pptx\package.py", line 151, in get_or_add_image_part
    image = Image.from_file(image_file)
  File "C:\Users\BattleShip\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pptx\parts\image.py", line 170, in from_file
    blob = image_file.read()
  File "C:\Users\BattleShip\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\PIL\Image.py", line 517, in __getattr__
    raise AttributeError(name)
AttributeError: read

CodePudding user response:

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.

  • Related