亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python實(shí)現(xiàn)模擬鍵盤鼠標(biāo)重復(fù)性操作Pyautogui

 更新時(shí)間:2023年11月06日 09:22:41   作者:柒月VII  
這篇文章主要為大家詳細(xì)介紹了python如何利用Pyautogui模擬鍵盤鼠標(biāo)重復(fù)性操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、程序樣式展示

將程序與cmd.xls文件放在同一文件夾,每一步的截圖也放在當(dāng)前文件夾通過圖片在屏幕上面進(jìn)行比對(duì),找到點(diǎn)擊處進(jìn)行自動(dòng)化操作

自動(dòng)化rpa測(cè)試

二、核心點(diǎn)

1.Pyautogui模塊:主要針對(duì)圖片進(jìn)行定位pyautogui.locateCenterOnScreen(),在屏幕上面找到該圖片位置后進(jìn)行pyautogui.click單擊,雙擊,右鍵,輸入操作,還有滑輪操作pyautogui.scroll,組合按鍵按鍵操作pyautogui.press(‘enter’),pyautogui.hotkey(),這里使用滑輪需要先點(diǎn)擊到滑輪處,然后進(jìn)行滑動(dòng)才行,不然可能會(huì)失效。

def mouseClick(clickTimes,lOrR,img,reTry):
    if reTry == 1:
        while True:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
                break
            print("未找到匹配圖片,0.1秒后重試")
            time.sleep(0.1)
    elif reTry == -1:
        while True:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
            time.sleep(0.1)
    elif reTry > 1:
        i = 1
        while i < reTry + 1:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
                print("重復(fù)")
                i += 1
            time.sleep(0.1)
def mainWork(img):
    i = 1
    while i < sheet1.nrows:
        #取本行指令的操作類型
        cmdType = sheet1.row(i)[0]
        if cmdType.value == 1.0:
            #取圖片名稱
            img = sheet1.row(i)[1].value
            reTry = 1
            if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
                reTry = sheet1.row(i)[2].value
            mouseClick(1,"left",img,reTry)
            print("單擊左鍵",img)
        #2代表雙擊左鍵
        elif cmdType.value == 2.0:
            #取圖片名稱
            img = sheet1.row(i)[1].value
            #取重試次數(shù)
            reTry = 1
            if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
                reTry = sheet1.row(i)[2].value
            mouseClick(2,"left",img,reTry)
            print("雙擊左鍵",img)
        #3代表右鍵
        elif cmdType.value == 3.0:
            #取圖片名稱
            img = sheet1.row(i)[1].value
            #取重試次數(shù)
            reTry = 1
            if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
                reTry = sheet1.row(i)[2].value
            mouseClick(1,"right",img,reTry)
            print("右鍵",img) 
        #4代表輸入
        elif cmdType.value == 4.0:
            inputValue = sheet1.row(i)[1].value
            pyperclip.copy(inputValue)
            pyautogui.hotkey('ctrl','v')
            time.sleep(0.5)
            print("輸入:",inputValue)                                        
        #5代表等待
        elif cmdType.value == 5.0:
            #取圖片名稱
            waitTime = sheet1.row(i)[1].value
            time.sleep(waitTime)
            print("等待",waitTime,"秒")
        #6代表滾輪
        elif cmdType.value == 6.0:
            #取圖片名稱
            scroll = sheet1.row(i)[1].value
            pyautogui.scroll(int(scroll))
            # pywinauto.mouse.scroll((700,800),-1000)
            print("滾輪滑動(dòng)",int(scroll),"距離")  
        elif cmdType.value == 7.0:
            key = sheet1.row(i)[1].value     
            pyautogui.press(key)
            time.sleep(0.5)         
            print('按下',key)  
        elif cmdType.value == 8.0:
            comkey = sheet1.row(i)[1].value 
            comkey = comkey.split('+')
            pyautogui.hotkey(comkey[0],comkey[1])
            print('按下',comkey[0] ,'+',comkey[1] )
        i += 1

2.讀取excel文件進(jìn)行數(shù)據(jù)驗(yàn)證,針對(duì)字符類型以及數(shù)字范圍進(jìn)行限制

def dataCheck(sheet1):
    checkCmd = True
    #行數(shù)檢查
    if sheet1.nrows<2:
        print("沒有數(shù)據(jù)")
        checkCmd = False
    #每行數(shù)據(jù)檢查
    i = 1
    while i < sheet1.nrows:
        # 第1列 操作類型檢查
        cmdType = sheet1.row(i)[0]
        if cmdType.ctype != 2 or (cmdType.value != 1.0 and cmdType.value != 2.0 and cmdType.value != 3.0 
        and cmdType.value != 4.0 and cmdType.value != 5.0 and cmdType.value != 6.0 and cmdType.value != 7.0 and cmdType.value != 8.0):
            print('第',i+1,"行,第1列數(shù)據(jù)有毛病")
            checkCmd = False
        # 第2列 內(nèi)容檢查
        cmdValue = sheet1.row(i)[1]
        # 讀圖點(diǎn)擊類型指令,內(nèi)容必須為字符串類型
        if cmdType.value ==1.0 or cmdType.value == 2.0 or cmdType.value == 3.0:
            if cmdValue.ctype != 1:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        # 輸入類型,內(nèi)容不能為空
        if cmdType.value == 4.0:
            if cmdValue.ctype == 0:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        # 等待類型,內(nèi)容必須為數(shù)字
        if cmdType.value == 5.0:
            if cmdValue.ctype != 2:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        # 滾輪事件,內(nèi)容必須為數(shù)字
        if cmdType.value == 6.0:
            if cmdValue.ctype != 2:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        #單獨(dú)按鍵enter
        if cmdType.value == 7.0:
            if cmdValue.ctype != 1:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        #組合鍵 ctrl+a ctrl+c  ctrl+v.... 
        if cmdType.value == 8.0:
            if cmdValue.ctype == 0:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        i += 1
    return checkCmd

三、完整代碼

import pyautogui
import time
import xlrd
import pyperclip
import pywinauto.mouse
#定義鼠標(biāo)事件
#pyautogui庫其他用法 https://blog.csdn.net/qingfengxd1/article/details/108270159
def mouseClick(clickTimes,lOrR,img,reTry):
    if reTry == 1:
        while True:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
                break
            print("未找到匹配圖片,0.1秒后重試")
            time.sleep(0.1)
    elif reTry == -1:
        while True:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
            time.sleep(0.1)
    elif reTry > 1:
        i = 1
        while i < reTry + 1:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
                print("重復(fù)")
                i += 1
            time.sleep(0.1)
# 數(shù)據(jù)檢查
# cmdType.value  1.0 左鍵單擊    2.0 左鍵雙擊  3.0 右鍵單擊  4.0 輸入  5.0 等待  6.0 滾輪
# ctype     空:0
#           字符串:1
#           數(shù)字:2
#           日期:3
#           布爾:4
#           error:5
def dataCheck(sheet1):
    checkCmd = True
    #行數(shù)檢查
    if sheet1.nrows<2:
        print("沒有數(shù)據(jù)")
        checkCmd = False
    #每行數(shù)據(jù)檢查
    i = 1
    while i < sheet1.nrows:
        # 第1列 操作類型檢查
        cmdType = sheet1.row(i)[0]
        if cmdType.ctype != 2 or (cmdType.value != 1.0 and cmdType.value != 2.0 and cmdType.value != 3.0 
        and cmdType.value != 4.0 and cmdType.value != 5.0 and cmdType.value != 6.0 and cmdType.value != 7.0 and cmdType.value != 8.0):
            print('第',i+1,"行,第1列數(shù)據(jù)有毛病")
            checkCmd = False
        # 第2列 內(nèi)容檢查
        cmdValue = sheet1.row(i)[1]
        # 讀圖點(diǎn)擊類型指令,內(nèi)容必須為字符串類型
        if cmdType.value ==1.0 or cmdType.value == 2.0 or cmdType.value == 3.0:
            if cmdValue.ctype != 1:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        # 輸入類型,內(nèi)容不能為空
        if cmdType.value == 4.0:
            if cmdValue.ctype == 0:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        # 等待類型,內(nèi)容必須為數(shù)字
        if cmdType.value == 5.0:
            if cmdValue.ctype != 2:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        # 滾輪事件,內(nèi)容必須為數(shù)字
        if cmdType.value == 6.0:
            if cmdValue.ctype != 2:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        #單獨(dú)按鍵enter
        if cmdType.value == 7.0:
            if cmdValue.ctype != 1:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        #組合鍵 ctrl+a ctrl+c  ctrl+v.... 
        if cmdType.value == 8.0:
            if cmdValue.ctype == 0:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        i += 1
    return checkCmd
#任務(wù)
def mainWork(img):
    i = 1
    while i < sheet1.nrows:
        #取本行指令的操作類型
        cmdType = sheet1.row(i)[0]
        if cmdType.value == 1.0:
            #取圖片名稱
            img = sheet1.row(i)[1].value
            reTry = 1
            if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
                reTry = sheet1.row(i)[2].value
            mouseClick(1,"left",img,reTry)
            print("單擊左鍵",img)
        #2代表雙擊左鍵
        elif cmdType.value == 2.0:
            #取圖片名稱
            img = sheet1.row(i)[1].value
            #取重試次數(shù)
            reTry = 1
            if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
                reTry = sheet1.row(i)[2].value
            mouseClick(2,"left",img,reTry)
            print("雙擊左鍵",img)
        #3代表右鍵
        elif cmdType.value == 3.0:
            #取圖片名稱
            img = sheet1.row(i)[1].value
            #取重試次數(shù)
            reTry = 1
            if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
                reTry = sheet1.row(i)[2].value
            mouseClick(1,"right",img,reTry)
            print("右鍵",img) 
        #4代表輸入
        elif cmdType.value == 4.0:
            inputValue = sheet1.row(i)[1].value
            pyperclip.copy(inputValue)
            pyautogui.hotkey('ctrl','v')
            time.sleep(0.5)
            print("輸入:",inputValue)                                        
        #5代表等待
        elif cmdType.value == 5.0:
            #取圖片名稱
            waitTime = sheet1.row(i)[1].value
            time.sleep(waitTime)
            print("等待",waitTime,"秒")
        #6代表滾輪
        elif cmdType.value == 6.0:
            #取圖片名稱
            scroll = sheet1.row(i)[1].value
            pyautogui.scroll(int(scroll))
            # pywinauto.mouse.scroll((700,800),-1000)
            print("滾輪滑動(dòng)",int(scroll),"距離")  
        elif cmdType.value == 7.0:
            key = sheet1.row(i)[1].value     
            pyautogui.press(key)
            time.sleep(0.5)         
            print('按下',key)  
        elif cmdType.value == 8.0:
            comkey = sheet1.row(i)[1].value 
            comkey = comkey.split('+')
            pyautogui.hotkey(comkey[0],comkey[1])
            print('按下',comkey[0] ,'+',comkey[1] )
        i += 1
if __name__ == '__main__':
    file = 'cmd.xls'
    #打開文件
    wb = xlrd.open_workbook(filename=file)
    #通過索引獲取表格sheet頁
    # sheet1 = wb.sheet_by_index(0)
    print('歡迎使用Henry自動(dòng)化工具')
    sheetname = input('請(qǐng)輸入需要執(zhí)行的工作表表名: ')
    sheet1 = wb.sheet_by_name(sheetname) 
    #數(shù)據(jù)檢查   
    checkCmd = dataCheck(sheet1)
    if checkCmd: 
        key=input('選擇功能: 1.執(zhí)行一次 2.循環(huán)到死 \n')
        if key=='1':
            #循環(huán)拿出每一行指令
            mainWork(sheet1)
        elif key=='2':
            while True:
                mainWork(sheet1)
                time.sleep(0.2)
                print("等待0.2秒")    
    else:
        print('輸入有誤或者已經(jīng)退出!')

以上就是python實(shí)現(xiàn)模擬鍵盤鼠標(biāo)重復(fù)性操作Pyautogui的詳細(xì)內(nèi)容,更多關(guān)于python模擬鍵盤鼠標(biāo)操作的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論