Python save xlPicture from clipboard

梦想的初衷 提交于 2019-12-23 04:01:04

问题


Currently I have an xlPicture saved in my clipboard from the call (via win32com):

ws.Range(ws.Cells(1,1),ws.Cells(8+rows,total_length)).CopyPicture() #Copies xlPicture to clipboard

Now I want to save the image in my clipboard to a file, so I tried using PIL:

    from PIL import ImageGrab
    img = ImageGrab.grabclipboard()
    img.save(os.path.join(r'C:\Windows\Temp\WS_Template_Images','test.png'),'PNG')

But ImageGrab.grabclipboard() returns None, I assume xlPicture is somehow not a compatible type for the call. Is there anything I can change to use ImageGrab or is there an alternate solution completely to saving an xlPicture? Thanks!


回答1:


It Turns out I can achieve this with the simple work around of :

ws.Range(ws.Cells(1,1),ws.Cells(8+rows,total_length)).CopyPicture()    
wb = excel.Workbooks.Add()
ws = wb.ActiveSheet
ws.Paste()
ws.Shapes('Picture 1').Copy()
img = ImageGrab.grabclipboard()
imgFile = os.path.join(r'C:\Windows\Temp\WS_Template_Images','test.png')
img.save(imgFile)

Pasting the image in a new workbook and then recopying and saving works, because this way the cell data is unlinked and it is considered an Image which ImageGrab can now get.



来源:https://stackoverflow.com/questions/17282278/python-save-xlpicture-from-clipboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!