赞
踩
@[TOC](这里写目录标题)
# 前言
例如:RPA机器人和按键精灵等技术或软件上的辅助,可以记录人工操作的步骤,做一些重复的工作。但是个人使用起来不是很灵活,一方面是软件收费,另一方面是办公安装限制。因此,尝试通过EXCEL来写控制命令,通过python进行执行,来辅助工作或娱乐等任务。
该方法效果:目前版本通过屏幕位点(X,Y)移动,通过中文指令进行模拟鼠标单击、双击、输入文字、左键按住、左键松开、键盘按键等操作,并每一步设置等待时间。代码详情如下:
1、tkinter:制作简单的操作界面;
2、pyautogui:模拟鼠标和键盘的操作
3、pandas:读取表格数据用
4、os:读取和保存用
- #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()

```
1、运行代码
2、选择写好操作的excel文件
3、点开始运行,想停止就把鼠标快速挪到左上角。
此外,
1)方式写0,代表用像素位点去定位,方式写其他数字,目前是啥都不干;
2)点击位置可以通过截图工具查看,截图时都能看到坐标是多少;
3)等待时间是执行完当行操作后等待多久去下一行,单位为秒;
4)操作目前有单击、双击、输入、左键按住、左键松开、按键、停止这几个操作;
模板和可执行版本程序可以从链接里下载
https://download.csdn.net/download/weixin_44283188/89123517?spm=1001.2014.3001.5501
程序所需的是这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 |
1、某游戏固定任务(自由发挥)
比如某任务,需要点某任务、接任务、进行任务、完成后再去找某人接任务,循环进行。
(只用到单击一个操作)
2、工作特定情境-图形化批量建模
工作某特定情况时,需要手动建立数十个模型小图标,调整到合适大小,并按顺序整齐排列。手动操作,每个图标会有大有小,不整齐。在excel表中计算好所需操作后,形成类似如下的操作表,确定好一个位点后,其他位点可以算出来。
3、其他情景
比如连续点击,定时点击,刷课不息屏等。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。