Python-pptx slide.shapes.add_picture() 无法识别图像文件 [英] Python-pptx slide.shapes.add_picture() not recognizing image files
问题描述
我正在尝试使用 pptx 将图像添加到 PowerPoint 幻灯片.
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)
每次我运行这个(我尝试了几个不同的图像,一些是由 matplotlib 生成的,一些是下载的)我从 .add_picture() 收到以下错误:
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?
推荐答案
这看起来像是与 PIL/Pillow 库相关的问题.您能否详细介绍一下您工作的环境以及您使用的 PIL 版本?我知道同时安装 PIL 和 Pillow 时存在一些潜在问题.如果您可以控制自己的 Python 环境,我会先卸载两者,然后只安装最新版本的 Pillow.
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.
这篇关于Python-pptx slide.shapes.add_picture() 无法识别图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!