python实现模拟键盘鼠标重复性操作Pyautogui

 更新时间:2023年11月06日 09:22:41   作者:柒月VII  
这篇文章主要为大家详细介绍了python如何利用Pyautogui模拟键盘鼠标重复性操作,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

一、程序样式展示

将程序与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("重复")
                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
            #取重试次数
            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
            #取重试次数
            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文件进行数据验证,针对字符类型以及数字范围进行限制

def dataCheck(sheet1):
    checkCmd = True
    #行数检查
    if sheet1.nrows<2:
        print("没有数据")
        checkCmd = False
    #每行数据检查
    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列数据有毛病")
            checkCmd = False
        # 第2列 内容检查
        cmdValue = sheet1.row(i)[1]
        # 读图点击类型指令,内容必须为字符串类型
        if cmdType.value ==1.0 or cmdType.value == 2.0 or cmdType.value == 3.0:
            if cmdValue.ctype != 1:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        # 输入类型,内容不能为空
        if cmdType.value == 4.0:
            if cmdValue.ctype == 0:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        # 等待类型,内容必须为数字
        if cmdType.value == 5.0:
            if cmdValue.ctype != 2:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        # 滚轮事件,内容必须为数字
        if cmdType.value == 6.0:
            if cmdValue.ctype != 2:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        #单独按键enter
        if cmdType.value == 7.0:
            if cmdValue.ctype != 1:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        #组合键 ctrl+a ctrl+c  ctrl+v.... 
        if cmdType.value == 8.0:
            if cmdValue.ctype == 0:
                print('第',i+1,"行,第2列数据有毛病")
                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("重复")
                i += 1
            time.sleep(0.1)
# 数据检查
# cmdType.value  1.0 左键单击    2.0 左键双击  3.0 右键单击  4.0 输入  5.0 等待  6.0 滚轮
# ctype     空:0
#           字符串:1
#           数字:2
#           日期:3
#           布尔:4
#           error:5
def dataCheck(sheet1):
    checkCmd = True
    #行数检查
    if sheet1.nrows<2:
        print("没有数据")
        checkCmd = False
    #每行数据检查
    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列数据有毛病")
            checkCmd = False
        # 第2列 内容检查
        cmdValue = sheet1.row(i)[1]
        # 读图点击类型指令,内容必须为字符串类型
        if cmdType.value ==1.0 or cmdType.value == 2.0 or cmdType.value == 3.0:
            if cmdValue.ctype != 1:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        # 输入类型,内容不能为空
        if cmdType.value == 4.0:
            if cmdValue.ctype == 0:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        # 等待类型,内容必须为数字
        if cmdType.value == 5.0:
            if cmdValue.ctype != 2:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        # 滚轮事件,内容必须为数字
        if cmdType.value == 6.0:
            if cmdValue.ctype != 2:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        #单独按键enter
        if cmdType.value == 7.0:
            if cmdValue.ctype != 1:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        #组合键 ctrl+a ctrl+c  ctrl+v.... 
        if cmdType.value == 8.0:
            if cmdValue.ctype == 0:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        i += 1
    return checkCmd
#任务
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
            #取重试次数
            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
            #取重试次数
            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('请输入需要执行的工作表表名: ')
    sheet1 = wb.sheet_by_name(sheetname) 
    #数据检查   
    checkCmd = dataCheck(sheet1)
    if checkCmd: 
        key=input('选择功能: 1.执行一次 2.循环到死 \n')
        if key=='1':
            #循环拿出每一行指令
            mainWork(sheet1)
        elif key=='2':
            while True:
                mainWork(sheet1)
                time.sleep(0.2)
                print("等待0.2秒")    
    else:
        print('输入有误或者已经退出!')

以上就是python实现模拟键盘鼠标重复性操作Pyautogui的详细内容,更多关于python模拟键盘鼠标操作的资料请关注脚本之家其它相关文章!

相关文章

  • 使用Python多线程爬虫爬取电影天堂资源

    使用Python多线程爬虫爬取电影天堂资源

    这篇文章主要介绍了使用Python多线程爬虫爬取电影天堂资源 的相关资料,需要的朋友可以参考下
    2016-09-09
  • Python操作MySQL的方法详细解读

    Python操作MySQL的方法详细解读

    这篇文章主要介绍了Python操作MySQL的方法详细解读,在Python中,通过使用第三方库:pymysql,完成对MySQL数据库的操作,Python操作MySQL并不难,难点是如何编写合适的SQL语句,需要的朋友可以参考下
    2023-11-11
  • Python如何处理异常报错方法(建议收藏!)

    Python如何处理异常报错方法(建议收藏!)

    开发程序其实就像预测天气一样,即使是代码的异常错误,也应该能预测且被控制,下面这篇文章主要给大家介绍了关于Python如何处理异常报错方法的相关资料,需要的朋友可以参考下
    2022-06-06
  • Python做文本按行去重的实现方法

    Python做文本按行去重的实现方法

    每行在promotion后面包含一些数字,如果这些数字是相同的,则认为是相同的行,对于相同的行,只保留一行。接下来通过本文给大家介绍Python做文本按行去重的实现方法,感兴趣的朋友一起看看吧
    2016-10-10
  • Python ATM功能实现代码实例

    Python ATM功能实现代码实例

    这篇文章主要介绍了Python ATM功能实现代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • python列表与列表算法详解(2)

    python列表与列表算法详解(2)

    这篇文章主要介绍了Python的列表和列表算法,小编感觉这篇文章具有一定参考价值,需要的朋友可以了解下,希望能给你带来帮助
    2021-08-08
  • 基于Python制作一个端午节相关的小游戏

    基于Python制作一个端午节相关的小游戏

    端午节快乐,今天我将为大家带来一篇有关端午节的编程文章,希望能够为大家献上一份小小的惊喜,我们将会使用Python来实现一个与端午粽子相关的小应用程序,在本文中,我将会介绍如何用Python代码制做一个“粽子拆解器”,感兴趣的小伙伴欢迎阅读
    2023-06-06
  • python新手学习可变和不可变对象

    python新手学习可变和不可变对象

    在本篇文章里小编给大家分享了是一篇关于python可变对象和不可变对象的基础知识点内容,有需要的朋友们可以参考下。
    2020-06-06
  • python中lambda匿名函数详解

    python中lambda匿名函数详解

    大家好,本篇文章主要讲的是python中lambda匿名函数详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-02-02
  • Python时间处理模块time和datetime详解

    Python时间处理模块time和datetime详解

    本文详细介绍了Python中常用的时间处理模块time和datetime,time模块提供多种时间获取和转换功能,datetime模块则在time的基础上增加了日期和时间的组合处理,如datetime.now()获取当前日期时间,两个模块在日常编程中非常有用,尤其是在需要时间日期计算和转换的场景下
    2024-10-10

最新评论