Python excel2img 失败:OSError: 无法打开剪贴板

3

我正在使用 Windows 11 上的 Python 3,编写一个简单的代码,将 Excel 复制为图像并将其粘贴到 Word 文档中。我正在使用 excel2img 包来完成这个操作,但是代码无法正常工作,我遇到了以下错误:

Traceback (most recent call last):
  File "C:\Users\Matheus Seidel\OneDrive\NCEE Meus documentos\Arquivos padrão\Ponte\Pontes - Dimensionamento de viga.py", line 40, in <module>
    excel2img.export_img("Vão 1 - Viga interna.xlsx", "esforços.png", "", "'Esforços solicitantes'!A1:K48")
  File "C:\Users\Matheus Seidel\AppData\Local\Programs\Python\Python311\Lib\site-packages\excel2img\excel2img.py", line 121, in export_img
    im = ImageGrab.grabclipboard()
         ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Matheus Seidel\AppData\Local\Programs\Python\Python311\Lib\site-packages\PIL\ImageGrab.py", line 112, in grabclipboard
    fmt, data = Image.core.grabclipboard_win32()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: failed to open clipboard

同样的代码在另一台Windows电脑上可以运行,所以问题肯定出在我的系统上,而不是代码中。

有人知道如何解决吗?

谢谢。

我尝试重新安装excel2img,但它没有起作用。

1个回答

0
我遇到了同样的问题。我只是添加了time.sleep(1)函数,然后它就起作用了。
def thread_function(name):
global lastcopy
while True:
    curcopy=ImageGrab.grabclipboard()
    if  curcopy != lastcopy:
        pegar_imagen()
        lastcopy=curcopy
    time.sleep(1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接