2

我在网上搜索了很多方法,但仍然无法解决。我是python的初学者。请帮我看看。

import hashlib
import time
import traceback
import win32api
import win32clipboard as clip
from io import BytesIO
import win32con
from PIL import Image


def setImage(data):
    m = hashlib.md5()
    try:
        clip.OpenClipboard()  
        clip.EmptyClipboard()  
        clip.SetClipboardData(win32con.CF_DIB, data) 
        m.update(clip.GetClipboardData(win32con.CF_DIB))
        clip.CloseClipboard()
        return m.hexdigest()
    except:
        traceback.print_exc()
        setImage(data)

def copyPicMain(imagePath, width = 600, height = 480, internal=0,    
 notResize = False):
    m = hashlib.md5()
    try:
        clip.OpenClipboard()
        m.update(clip.GetClipboardData(win32con.CF_DIB))
        clip.CloseClipboard()
        pre_md5 = m.hexdigest()
    except TypeError:
        # if clip is empty
        traceback.print_exc()
        print('empty')
        pre_md5 = ''
    except:
        traceback.print_exc()
        clip.OpenClipboard()
        m.update(clip.GetClipboardData(win32con.CF_DIB))
        clip.CloseClipboard()
        pre_md5 = m.hexdigest()
    img = Image.open(imagePath)
    output = BytesIO()
    if not notResize:
        img = img.resize((height, width), Image.BILINEAR)
    img.convert("RGB").save(output, "BMP")
    data = output.getvalue()[14:]
    output.close()
    new_md5 = setImage(data)
    print(pre_md5, new_md5)
    time.sleep(internal)
    win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)  # ctrl key code 17
    win32api.keybd_event(86, 0, 0, 0)  # v key code 86
    win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)  # key up 
    time.sleep(internal)

if __name__ == '__main__':
    imagePathList =    [r'C:\Users\strives\Desktop\panda.png',r'C:\Users\strives\Desktop\black.png',r'C:\Users\strives\Desktop\1.png']
for imagePath in imagePathList:
    copyPicMain(imagePath)

调试结果:

C:\Users\strives>python C:\Users\strives\Desktop\test.py Traceback(最近一次调用最后):

文件“C:\Users\strives\Desktop\test.py”,第 28 行,在 copyPicMain m.update(clip.GetClipboardData(win32con.CF_DIB))

TypeError:指定的剪贴板格式不可用

空的

回溯(最近一次通话最后):

文件“C:\Users\strives\AppData\Local\Programs\Python\Python37-32\lib\site-

packages\PIL\Image.py",第 2656 行,打开 fp.seek(0)

AttributeError: 'list' 对象没有属性 'seek'

在处理上述异常的过程中,又出现了一个异常:

回溯(最近一次通话最后):

文件“C:\Users\strives\Desktop\test.py”,第 60 行,在 copyPicMain(imagePathList)

文件“C:\Users\strives\Desktop\test.py”,第 42 行,在 copyPicMain img = Image.open(imagePath)

文件“C:\Users\strives\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\Image.py”,第 2658 行,打开 fp = io.BytesIO(fp.read() )

AttributeError:“列表”对象没有属性“读取”

编辑

我重新编译它并出现以下错误。

C:\Users\strives>python C:\Users\strives\Desktop\test.py

cfdb447aa6b20d45ba79b5802a677454 1aa0384b01ed1e2ecd6158c52a1355a5 1aa0384b01ed1e2ecd6158c52a1355a5 c28f8947d15edb17752ec5f503a1b82b

回溯(最近一次通话最后):

文件“C:\Users\strives\Desktop\test.py”,第 27 行,在 copyPicMain clip.OpenClipboard()

pywintypes.error: (5, 'OpenClipboard', 'Access is denied.') c28f8947d15edb17752ec5f503a1b82b cfdb447aa6b20d45ba79b5802a677454

4

1 回答 1

0

你犯了一个错误

                     for imagePath in imagePathList:
                          copyPicMain(imagePathList)

您应该将 imagePath 作为参数而不是完整列表发送,例如:

                     for imagePath in imagePathList:
                          copyPicMain(imagePath)

希望有帮助:)

于 2019-04-19T05:24:38.083 回答