问题
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