如何在python-pptx中将PIL图像传递到Add_Picture [英] How to pass PIL image to Add_Picture in python-pptx

查看:538
本文介绍了如何在python-pptx中将PIL图像传递到Add_Picture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从剪贴板中获取图像,我想将该图像添加到python-pptx中. 我不想将图像保存在磁盘中. 我已经尝试过了:

I'm trying to get the image from clipboard and I want to add that image in python-pptx . I don't want to save the image in the Disk. I have tried this:

from pptx import Presentation
from PIL import ImageGrab,Image
from pptx.util import Inches
im = ImageGrab.grabclipboard()
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
left = top = Inches(1)
pic = slide.shapes.add_picture(im, left, top)
prs.save('PPT.pptx')

但是出现此错误

File "C:\Python27\lib\site-packages\PIL\Image.py", line 627, in __getattr__
    raise AttributeError(name)
AttributeError: read

这有什么问题?

推荐答案

这对我有用

import io
import PIL
from pptx import Presentation
from pptx.util import Inches

# already have a PIL.Image as image
prs = Presentation()
blank_slide = prs.slide_layout[6]
left = top = Inches(0)

# I had this part in a loop so that I could put one generated image per slide
image: PIL.Image = MyFunctionToGetImage()
slide = prs.slides.add_slide(blank_slide)
with io.BytesIO() as output:
    image.save(output, format="GIF")
    pic = slides.add_slide(output, left, top)
# end loop
prs.save("my.pptx")

这篇关于如何在python-pptx中将PIL图像传递到Add_Picture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆