欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、程序樣式展示

將程序與cmd.xls文件放在同一文件夾,每一步的截圖也放在當(dāng)前文件夾通過(guò)圖片在屏幕上面進(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:
        #取本行指令的操作類(lèi)型
        cmdType = sheet1.row(i)[0]
        if cmdType.value == 1.0:
            #取圖片名稱(chēng)
            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:
            #取圖片名稱(chēng)
            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:
            #取圖片名稱(chēng)
            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:
            #取圖片名稱(chēng)
            waitTime = sheet1.row(i)[1].value
            time.sleep(waitTime)
            print("等待",waitTime,"秒")
        #6代表滾輪
        elif cmdType.value == 6.0:
            #取圖片名稱(chēng)
            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ì)字符類(lèi)型以及數(shù)字范圍進(jìn)行限制

def dataCheck(sheet1):
    checkCmd = True
    #行數(shù)檢查
    if sheet1.nrows<2:
        print("沒(méi)有數(shù)據(jù)")
        checkCmd = False
    #每行數(shù)據(jù)檢查
    i = 1
    while i < sheet1.nrows:
        # 第1列 操作類(lèi)型檢查
        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)擊類(lèi)型指令,內(nèi)容必須為字符串類(lè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
        # 輸入類(lèi)型,內(nèi)容不能為空
        if cmdType.value == 4.0:
            if cmdValue.ctype == 0:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        # 等待類(lèi)型,內(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庫(kù)其他用法 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("沒(méi)有數(shù)據(jù)")
        checkCmd = False
    #每行數(shù)據(jù)檢查
    i = 1
    while i < sheet1.nrows:
        # 第1列 操作類(lèi)型檢查
        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)擊類(lèi)型指令,內(nèi)容必須為字符串類(lè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
        # 輸入類(lèi)型,內(nèi)容不能為空
        if cmdType.value == 4.0:
            if cmdValue.ctype == 0:
                print('第',i+1,"行,第2列數(shù)據(jù)有毛病")
                checkCmd = False
        # 等待類(lèi)型,內(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:
        #取本行指令的操作類(lèi)型
        cmdType = sheet1.row(i)[0]
        if cmdType.value == 1.0:
            #取圖片名稱(chēng)
            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:
            #取圖片名稱(chēng)
            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:
            #取圖片名稱(chēng)
            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:
            #取圖片名稱(chēng)
            waitTime = sheet1.row(i)[1].value
            time.sleep(waitTime)
            print("等待",waitTime,"秒")
        #6代表滾輪
        elif cmdType.value == 6.0:
            #取圖片名稱(chēng)
            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'
    #打開(kāi)文件
    wb = xlrd.open_workbook(filename=file)
    #通過(guò)索引獲取表格sheet頁(yè)
    # 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)模擬鍵盤(pán)鼠標(biāo)重復(fù)性操作Pyautogui的詳細(xì)內(nèi)容,更多關(guān)于python模擬鍵盤(pán)鼠標(biāo)操作的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python對(duì)Excel不同的行分別復(fù)制不同的次數(shù)

    Python對(duì)Excel不同的行分別復(fù)制不同的次數(shù)

    這篇文章主要介紹了如何利用Python實(shí)現(xiàn)讀取Excel表格文件數(shù)據(jù),并將其中符合我們特定要求的那一行加以復(fù)制指定的次數(shù),感興趣的小伙伴可以學(xué)習(xí)一下
    2023-07-07
  • Pycharm創(chuàng)建python文件自動(dòng)添加日期作者等信息(步驟詳解)

    Pycharm創(chuàng)建python文件自動(dòng)添加日期作者等信息(步驟詳解)

    這篇文章主要介紹了Pycharm創(chuàng)建python文件自動(dòng)添加日期作者等信息(步驟詳解),本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • pygame 鍵盤(pán)事件的實(shí)踐

    pygame 鍵盤(pán)事件的實(shí)踐

    本文主要介紹了pygame 鍵盤(pán)事件,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • python使用yield壓平嵌套字典的超簡(jiǎn)單方法

    python使用yield壓平嵌套字典的超簡(jiǎn)單方法

    這篇文章主要給大家介紹了關(guān)于python使用yield壓平嵌套字典的超簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • matplotlib多子圖實(shí)現(xiàn)共享坐標(biāo)軸的示例詳解

    matplotlib多子圖實(shí)現(xiàn)共享坐標(biāo)軸的示例詳解

    這篇文章主要為大家詳細(xì)介紹了matplotlib繪制多子圖師如何實(shí)現(xiàn)共享坐標(biāo)軸,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • 釘釘群自定義機(jī)器人消息Python封裝的實(shí)例

    釘釘群自定義機(jī)器人消息Python封裝的實(shí)例

    今天小編就為大家分享一篇釘釘群自定義機(jī)器人消息Python封裝的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • python DataFrame獲取行數(shù)、列數(shù)、索引及第幾行第幾列的值方法

    python DataFrame獲取行數(shù)、列數(shù)、索引及第幾行第幾列的值方法

    下面小編就為大家分享一篇python DataFrame獲取行數(shù)、列數(shù)、索引及第幾行第幾列的值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • python使用urlparse分析網(wǎng)址中域名的方法

    python使用urlparse分析網(wǎng)址中域名的方法

    這篇文章主要介紹了python使用urlparse分析網(wǎng)址中域名的方法,涉及Python使用urlparse模塊操作URL的技巧,需要的朋友可以參考下
    2015-04-04
  • python生成驗(yàn)證碼圖片代碼分享

    python生成驗(yàn)證碼圖片代碼分享

    這篇文章主要為大家介紹了python生成驗(yàn)證碼圖片代碼,生成原理是將一串隨機(jī)產(chǎn)生的數(shù)字或符號(hào),生成一幅圖片,圖片里加上一些干擾象素,想要實(shí)現(xiàn)驗(yàn)證碼圖片的朋友可以參考一下
    2016-01-01
  • python并發(fā)2之使用asyncio處理并發(fā)

    python并發(fā)2之使用asyncio處理并發(fā)

    本篇文章主要介紹了python并發(fā)2之使用asyncio處理并發(fā),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12

最新評(píng)論