【Python自动化控制电脑(模仿鼠标和键盘)】

【Python自动化控制电脑(模仿鼠标和键盘)】本文介绍了如何通过 Python 编程 结合 Tkinter pyautogui 等库 使用 Excel 作为配置文件 实现对屏幕操作的自动化 包括鼠标点击 输入 键盘按键等 适用于游戏任务自动化 图形化批量建模等场景

大家好,欢迎来到IT知识分享网。

例如:RPA机器人和按键精灵等技术或软件上的辅助,可以记录人工操作的步骤,做一些重复的工作。但是个人使用起来不是很灵活,一方面是软件收费,另一方面是办公安装限制。因此,尝试通过EXCEL来写控制命令,通过python进行执行,来辅助工作或娱乐等任务。

该方法效果:目前版本通过屏幕位点(X,Y)移动,通过中文指令进行模拟鼠标单击、双击、输入文字、左键按住、左键松开、键盘按键等操作,并每一步设置等待时间。代码详情如下:

一、用到的Python模块

#coding:utf-8 from tkinter import * from tkinter import filedialog #分割路径和文件名 import  os.path #读取excel文件模块 import pandas as pd #设想,读取excel列表,图片名称、单击、间隔时长、步骤说明 import pyautogui #RPA自动控制 pyautogui.FAILSAFE = True import time #控制时间 class MYauto():     def __init__(self,init_window_name):         #传递实例化的窗口         self.init_windows_name = init_window_name         #文件路径self.folder         #excel内容self.Tj         #是否找到         self.isfind = 0     def set_init_window(self):         # 设置窗体标题         self.init_windows_name.title('(鼠标滑到左上角,停止程序)Python GUI MY.')         # 设置窗口大小和位置         self.init_windows_name.geometry('500x120+570+200')         self.label1 = Label(self.init_windows_name, text='请选择文件:')         self.text1 = Entry(self.init_windows_name, bg='white', width=30)         self.button1 = Button(self.init_windows_name, text='浏览', width=8, command=self.selectExcelfile)         self.button2 = Button(self.init_windows_name, text='开始', width=8, command=self.main)         self.button3 = Button(self.init_windows_name, text='停止', width=8, command=self.stop)         self.label1.pack()         self.text1.pack()         self.button1.pack()         self.button2.pack()         self.button3.pack()         self.label1.place(x=30, y=30)         self.text1.place(x=120, y=30)         self.button1.place(x=400, y=26)         self.button2.place(x=160, y=80)         self.button3.place(x=260, y=80)     def selectExcelfile(self):         #获取excel数据         self.file_path = filedialog.askopenfilename(title='选择Excel文件模板', filetypes=[('Excel', '*.xlsx'), ('All Files', '*')])         print(self.file_path)         self.text1.insert(INSERT, self.file_path)         #记录路径文件夹         self.file_folder,f = os.path.split(self.file_path)     def stop(self):         # 是否继续         pass     def operation(self,opt,key):         if opt == "单击":             pyautogui.click(button='left')         if opt == "双击":             pyautogui.doubleClick(button='left')         if opt == "按键":             pyautogui.press(key)         if opt == "输入":             pyautogui.typewrite(message=key,interval=0.2)         #if opt == "拖拽":             #pyautogui.dragTo         if opt == "左键按住":             pyautogui.mouseDown()         if opt == "左键松开":             pyautogui.mouseUp()     def main(self):         #读取excel配置文件内容         self.Tj = pd.read_excel(self.file_path, sheet_name='steps').reset_index()         #是否继续         self.isstart = 1         while self.isstart == 1:             for i_order in range(len(self.Tj['序号'])):                 if self.Tj['操作'][i_order] == "停止":                     self.isstart = 0                     print("结束啦~~~")                     break                 if pyautogui.position() == (0,0):                     self.isstart = 0                     print("结束啦~~~")                     break                 if self.Tj['方式'][i_order] == 0:                     #通过位点操作                     pyautogui.moveTo(self.Tj['点击位置X'][i_order], self.Tj['点击位置Y'][i_order])                     #相对移动pyautogui.moveRel()                     #进行各种操作                     #pyautogui.click()#单击                     self.operation(self.Tj['操作'][i_order],self.Tj['输入'][i_order])                     #print(self.Tj['点击位置X'][i_order]+','+self.Tj['点击位置Y'][i_order])                     time.sleep(self.Tj['等待时间'][i_order])         pass if  __name__ == '__main__':     init_window = Tk()              #实例化出一个父窗口     MAIN_PROCESS = MYauto(init_window)     # 设置根窗口默认属性     MAIN_PROCESS.set_init_window()     init_window.mainloop()

二、使用方法

3)等待时间是执行完当行操作后等待多久去下一行,单位为秒;

4)操作目前有单击、双击、输入、左键按住、左键松开、按键、停止这几个操作;

三、表格模板

程序所需的是这6列,只要列名一致就行。然后可以自己加序号、备注及各种辅助列。

方式 点击位置X 点击位置Y 等待时间 操作 输入
0 296 193 0.1 单击
0 10 220 0.1 单击
0 50 260 0.1 双击
0 150 303 0.1 单击
0 150 303 0.1 输入 XXX24l

四、应用案例

3、其他情景

比如连续点击,定时点击,刷课不息屏等。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/125764.html

(0)
上一篇 2025-09-24 15:20
下一篇 2025-09-24 15:26

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信