最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、程序樣式展示

將程序與cmd.xls文件放在同一文件夾,每一步的截圖也放在當前文件夾通過圖片在屏幕上面進行比對,找到點擊處進行自動化操作

自動化rpa測試

二、核心點

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

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("滾輪滑動",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文件進行數(shù)據(jù)驗證,針對字符類型以及數(shù)字范圍進行限制

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]
        # 讀圖點擊類型指令,內(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
        #單獨按鍵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
#定義鼠標事件
#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]
        # 讀圖點擊類型指令,內(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
        #單獨按鍵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("滾輪滑動",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自動化工具')
    sheetname = input('請輸入需要執(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實現(xiàn)模擬鍵盤鼠標重復(fù)性操作Pyautogui的詳細內(nèi)容,更多關(guān)于python模擬鍵盤鼠標操作的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 使用Python多線程爬蟲爬取電影天堂資源

    使用Python多線程爬蟲爬取電影天堂資源

    這篇文章主要介紹了使用Python多線程爬蟲爬取電影天堂資源 的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Python操作MySQL的方法詳細解讀

    Python操作MySQL的方法詳細解讀

    這篇文章主要介紹了Python操作MySQL的方法詳細解讀,在Python中,通過使用第三方庫:pymysql,完成對MySQL數(shù)據(jù)庫的操作,Python操作MySQL并不難,難點是如何編寫合適的SQL語句,需要的朋友可以參考下
    2023-11-11
  • Python如何處理異常報錯方法(建議收藏!)

    Python如何處理異常報錯方法(建議收藏!)

    開發(fā)程序其實就像預(yù)測天氣一樣,即使是代碼的異常錯誤,也應(yīng)該能預(yù)測且被控制,下面這篇文章主要給大家介紹了關(guān)于Python如何處理異常報錯方法的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • Python做文本按行去重的實現(xiàn)方法

    Python做文本按行去重的實現(xiàn)方法

    每行在promotion后面包含一些數(shù)字,如果這些數(shù)字是相同的,則認為是相同的行,對于相同的行,只保留一行。接下來通過本文給大家介紹Python做文本按行去重的實現(xiàn)方法,感興趣的朋友一起看看吧
    2016-10-10
  • Python ATM功能實現(xiàn)代碼實例

    Python ATM功能實現(xiàn)代碼實例

    這篇文章主要介紹了Python ATM功能實現(xiàn)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • python列表與列表算法詳解(2)

    python列表與列表算法詳解(2)

    這篇文章主要介紹了Python的列表和列表算法,小編感覺這篇文章具有一定參考價值,需要的朋友可以了解下,希望能給你帶來幫助
    2021-08-08
  • 基于Python制作一個端午節(jié)相關(guān)的小游戲

    基于Python制作一個端午節(jié)相關(guān)的小游戲

    端午節(jié)快樂,今天我將為大家?guī)硪黄嘘P(guān)端午節(jié)的編程文章,希望能夠為大家獻上一份小小的驚喜,我們將會使用Python來實現(xiàn)一個與端午粽子相關(guān)的小應(yīng)用程序,在本文中,我將會介紹如何用Python代碼制做一個“粽子拆解器”,感興趣的小伙伴歡迎閱讀
    2023-06-06
  • python新手學(xué)習(xí)可變和不可變對象

    python新手學(xué)習(xí)可變和不可變對象

    在本篇文章里小編給大家分享了是一篇關(guān)于python可變對象和不可變對象的基礎(chǔ)知識點內(nèi)容,有需要的朋友們可以參考下。
    2020-06-06
  • python中l(wèi)ambda匿名函數(shù)詳解

    python中l(wèi)ambda匿名函數(shù)詳解

    大家好,本篇文章主要講的是python中l(wèi)ambda匿名函數(shù)詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Python時間處理模塊time和datetime詳解

    Python時間處理模塊time和datetime詳解

    本文詳細介紹了Python中常用的時間處理模塊time和datetime,time模塊提供多種時間獲取和轉(zhuǎn)換功能,datetime模塊則在time的基礎(chǔ)上增加了日期和時間的組合處理,如datetime.now()獲取當前日期時間,兩個模塊在日常編程中非常有用,尤其是在需要時間日期計算和轉(zhuǎn)換的場景下
    2024-10-10

最新評論

上饶市| 古丈县| 资阳市| 桃园县| 景宁| 芮城县| 镇江市| 南岸区| 大厂| 斗六市| 治多县| 册亨县| 张家川| 措美县| 仁布县| 克东县| 互助| 资源县| 宜章县| 建瓯市| 霍林郭勒市| 屯昌县| 兴化市| 仁寿县| 滨海县| 黄陵县| 邵阳县| 繁峙县| 华容县| 中西区| 桂平市| 乌兰察布市| 沧州市| 获嘉县| 长春市| 荥经县| 曲麻莱县| 郧西县| 侯马市| 连城县| 金塔县|