0

I am trying to create a pptx or powerpoint file using python-pptx and reading the image using python wand library but getting error like AttributeError: 'Image' object has no attribute 'seek'. Did you mean: 'seed'?

Note: All the files are in same folder starts with 'watermarked_'

from io import FileIO
import os
from wand.image import Image
from pptx.util import Inches 
from pptx import Presentation 

def create_slide()->FileIO:
    # Creating presentation object
    root = Presentation()
    for file in os.listdir():
        if file.startswith('watermarked_'):
            # Creating slide layout
            first_slide_layout = root.slide_layouts[1] 
            slide = root.slides.add_slide(first_slide_layout)
            shapes = slide.shapes
            
            #Adding title or heading to the slide
            title_shape = shapes.title
            title_shape.text = f" Created By python-pptx for Watermarking "
            
            #Adding sub-title with border to the slide
            body_shape = shapes.placeholders[1]
            tf = body_shape.text_frame
            tf.text = f"This is a watermarked image of {file}"
                
            with Image(filename = file) as watermarked_image:
                
                #Maintianing the aspect ratio of the image
                width, height = watermarked_image.size
                ratio = height/width
                new_width = width / 2
                new_height = int(new_width * ratio)
                watermarked_image.resize(int(new_width), new_height)
                
                # Add the watermarked image to the slide
                slide.shapes.add_picture(watermarked_image ,Inches(1), Inches(3))
                root.save("Output.pptx")

create_slide()

Traceback (most recent call last):
  File "/Users/quantum/Desktop/image/project.py", line 60, in <module>
quantum@MacBook-Air image % python -u "/Users/quantum/Desktop/image/project.py"
Traceback (most recent call last):
  File "/Users/quantum/Desktop/image/project.py", line 60, in <module>
    create_slide()
  File "/Users/quantum/Desktop/image/project.py", line 57, in create_slide
    slide.shapes.add_picture(watermarked_image ,Inches(1), Inches(3))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/shapes/shapetree.py", line 332, in add_picture
    image_part, rId = self.part.get_or_add_image_part(image_file)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/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 "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/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 "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/package.py", line 151, in get_or_add_image_part
    image = Image.from_file(image_file)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/parts/image.py", line 168, in from_file
    if callable(getattr(image_file, "seek")):
AttributeError: 'Image' object has no attribute 'seek'. Did you mean: 'seed'?

Any frequent help will be much appreciated

Md Tausif
  • 279
  • 1
  • 8

1 Answers1

1

A wand.image.Image object is not a valid argument for Shapes.add_picture(). The first argument to that call needs to be the str path to an image file or a file-like object containing an image.

I suppose that means you'll need to save the modified image as a JPG or PNG or whatever and then provide the filename. You could also save it to a BytesIO object and pass that to .add_picture() as that would count as a file-like object and not require using the filesystem.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • Thanks man finally i am able to do it by just adding buf = io.BytesIO() watermarked_image.save(buf ) buf.seek(0) # Add the watermarked image to the slide slide.shapes.add_picture(buf, Inches(1), Inches(1)) – Md Tausif Dec 09 '21 at 08:15
  • Glad you got it working @MdTausif, don't neglect to accept an answer that fixes your problem by clicking the checkmark to the left of the answer. That's how you say "Thank you" here on SO and it builds your reputation points as well. – scanny Dec 09 '21 at 18:24