python實(shí)現(xiàn)模擬鍵盤鼠標(biāo)重復(fù)性操作Pyautogui
一、程序樣式展示
將程序與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)文章!
- Python pyautogui模擬鍵盤輸入操作的示例詳解
- python pyautogui手動(dòng)活動(dòng)(模擬鼠標(biāo)鍵盤)自動(dòng)化庫使用
- python?PyAutoGUI實(shí)現(xiàn)自動(dòng)化鼠標(biāo)鍵盤等常用操作
- Python PyAutoGUI實(shí)現(xiàn)自動(dòng)化任務(wù)應(yīng)用場(chǎng)景示例
- python自動(dòng)化神器pyautogui使用步驟
- Python利用PyAutoGUI模塊實(shí)現(xiàn)控制鼠標(biāo)鍵盤
- Python利用PyAutoGUI模擬鼠標(biāo)鍵盤的原理解析和踩坑指南
相關(guān)文章
python讀寫excel數(shù)據(jù)--pandas詳解
這篇文章主要為大家詳細(xì)介紹了python操作EXCEL讀數(shù)據(jù)、寫數(shù)據(jù)的實(shí)例源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
安裝pytorch報(bào)錯(cuò)torch.cuda.is_available()=false問題的解決過程
最近想用pytorch,因此裝了pytorch,但是碰到了問題,下面這篇文章主要給大家介紹了關(guān)于安裝pytorch報(bào)錯(cuò)torch.cuda.is_available()=false問題的解決過程,需要的朋友可以參考下2022-05-05
Python機(jī)器學(xué)習(xí)10大經(jīng)典算法的講解和示例
10個(gè)經(jīng)典的機(jī)器學(xué)習(xí)算法包括:線性回歸、邏輯回歸、K-最近鄰(KNN)、支持向量機(jī)(SVM)、決策樹、隨機(jī)森林、樸素貝葉斯、K-均值聚類、主成分分析(PCA)、和梯度提升(Gradient?Boosting),我將使用常見的機(jī)器學(xué)習(xí)庫,如scikit-learn,numpy和pandas?來實(shí)現(xiàn)這些算法2024-06-06
Python實(shí)現(xiàn)打包成庫供別的模塊調(diào)用
這篇文章主要介紹了Python實(shí)現(xiàn)打包成庫供別的模塊調(diào)用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Python中的自定義函數(shù)學(xué)習(xí)筆記
這篇文章主要介紹了Python中的自定義函數(shù)學(xué)習(xí)筆記,本文講解了定義函數(shù)、callable函數(shù)、help函數(shù)等內(nèi)容,需要的朋友可以參考下2014-09-09
Python高級(jí)應(yīng)用探索之元編程和并發(fā)編程詳解
Python作為一種簡(jiǎn)單易用且功能強(qiáng)大的編程語言,廣泛應(yīng)用于各個(gè)領(lǐng)域,本文主要來和大家一起探索一下Python中的優(yōu)化技巧、元編程和并發(fā)編程,希望對(duì)大家有所幫助2023-11-11
詳解如何利用pandas進(jìn)行數(shù)據(jù)行轉(zhuǎn)列和列轉(zhuǎn)行
這篇文章主要為大家詳細(xì)介紹了如何利用pandas進(jìn)行數(shù)據(jù)行轉(zhuǎn)列和列轉(zhuǎn)行,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2007-02-02
對(duì)Python3之進(jìn)程池與回調(diào)函數(shù)的實(shí)例詳解
今天小編就為大家分享一篇對(duì)Python3之進(jìn)程池與回調(diào)函數(shù)的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Django 內(nèi)置權(quán)限擴(kuò)展案例詳解
這篇文章主要介紹了Django 內(nèi)置權(quán)限擴(kuò)展案例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

