How to pass PIL image to Add_Picture in python-ppt

2019-08-03 07:55发布

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')

But Getting this error

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

What is wrong with this?

1条回答
Fickle 薄情
2楼-- · 2019-08-03 08:22

The image needs to be in the form of a stream (i.e. logical file) object. So you need to "save" it to a memory file first, probably StringIO is what you're looking for.

This other question provides some of the details.

查看更多
登录 后发表回答