Python-pptx slide.shapes.add_picture() not recogni

2019-08-29 02:55发布

问题:

I am trying to add images to a PowerPoint slide using pptx.

img_path = 'test.png'

prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

left = top = Inches(1)
pic = slide.shapes.add_picture(img_path, left, top)

Every time I run this (I have tried with several different images, some generated by matplotlib and some downloaded) I receive the following error from the .add_picture():

Continuum\Anaconda\lib\site-packages\pptx\parts\image.pyc in _size(self)
    158         image_stream = StringIO(self._blob)
--> 159         width_px, height_px = PIL_Image.open(image_stream).size
    160         image_stream.close()
    161         return width_px, height_px

Continuum\Anaconda\lib\site-packages\PIL\Image.pyc in open(fp, mode)
   1978     :param size: A 2-tuple, containing (width, height) in pixels.
   1979     :param color: What color to use for the image.  Default is black.
-> 1980        If given, this should be a single integer or floating point value
   1981        for single-band modes, and a tuple for multi-band modes (one value
   1982        per band).  When creating RGB images, you can also use color

IOError: cannot identify image file

What can I do to fix this problem?

回答1:

This looks like a problem related to the PIL/Pillow library. Can you say more about the environment you're working in and what PIL version you're using? I know there are some potential problems when having both PIL and Pillow installed. If you have control over your Python environment I'd start by uninstalling both and then installing just the most recent version of Pillow.