我正在尝试使用pptx将图像添加到PowerPoint幻灯片。
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)
每次我运行这个(我尝试过几个不同的图像,一些是由matplotlib生成的,一些是下载的)我从.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
我该怎么做才能解决这个问题?
答案 0 :(得分:1)
这看起来像是与PIL / Pillow库有关的问题。您能否详细说明您正在使用的环境以及您正在使用的PIL版本?我知道安装PIL和Pillow时存在一些潜在的问题。如果您可以控制Python环境,我首先卸载两个,然后再安装最新版本的Pillow。