gpt4 book ai didi

Python从剪贴板保存xlPicture

转载 作者:太空宇宙 更新时间:2023-11-03 18:55:51 29 4
gpt4 key购买 nike

目前,我通过调用(通过 win32com)将 xlPicture 保存在剪贴板中:

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

现在我想将剪贴板中的图像保存到文件中,所以我尝试使用 PIL:

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

但是 ImageGrab.grabclipboard() 返回 None,我认为 xlPicture 在某种程度上与调用不兼容。有什么我可以更改以使用 ImageGrab 或者是否有完全替代的解决方案来保存 xlPicture?谢谢!

最佳答案

事实证明我可以通过以下简单的解决方法来实现这一目标:

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)

将图像粘贴到新工作簿中,然后重新复制并保存是有效的,因为这样单元格数据就被取消链接,并且它被认为是 ImageGrab 现在可以获取的图像。

关于Python从剪贴板保存xlPicture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17282278/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com