import os
import pyautogui
import time, os
import pyperclip # 复制
pyautogui.FAILSAFE = False class Auto:
def get_xs(self, x_imgs):
"""
:param x_imgs: 用来确定表格x坐标的图片 路径列表
:return:
"""
xs = []
for img in x_imgs:
button7location = None
while not button7location:
button7location = pyautogui.locateOnScreen(img, grayscale=True)
if button7location:
x, _ = [i / 2 for i in pyautogui.center(button7location)]
xs.append(x)
else:
if img == x_imgs[0]:
return xs
return xs def to_find_page(self):
print('切换网页')
pyautogui.hotkey('ctrl', 'tab') # 切换网页
# pyautogui.hotkey('command', 'tab') # 切换网页
time.sleep(1)
return True def get_x_imgs(self, x_path):
abs_file = os.path.abspath('.')
if not abs_file.endswith('img'):
abs_file = os.path.join(abs_file, 'utils/img')
print(abs_file)
path = os.path.join(abs_file, x_path)
print(path)
# x_imgs = [f'utils/img/{x_path}/x1.png', f'utils/img/{x_path}/x2.png',
# f'utils/img/{x_path}/x3.png', f'utils/img/{x_path}/x4.png']
# x_imgs = [os.path.join(abs_file, i) for i in x_imgs]
l = os.walk(path)
x_imgs = []
for i, v, files in l:
for file in files:
if file.split('.')[0].startswith('x') and file.split('.')[-1] in ['png', 'jpeg']:
x_imgs.append(os.path.join(path, file))
return x_imgs def auto_write(self, xs, datas, x_path):
"""
:param xs: 表格每列的x坐标列表
:param datas: 需要填写的数据 {y1_2: 100} y1:代表第一行, 2:代表第二列, 100为填写的值
:param x_path: 文件夹名字(以表格名字为文件夹名)
:return:
"""
keys = datas.keys()
# yes = False
for key in keys:
name, index = key.split('_')
# file = f'utils/img/{x_path}/{name}.png'
file = f'{x_path}/{name}.png'
abs_file = os.path.abspath('.')
if not abs_file.endswith('img'):
abs_file = os.path.join(abs_file, 'utils/img')
file = os.path.join(abs_file, file)
button7location = None
while button7location is None:
button7location = pyautogui.locateOnScreen(file, grayscale=True, confidence=.8)
# 滚动条下滑
if not button7location:
# if not yes:
# return False
pyautogui.scroll(-1)
# yes = True
_, button7y = [i / 2 for i in pyautogui.center(button7location)]
print(key)
pyautogui.moveTo(xs[int(index)-1], button7y, duration=.1) pyautogui.click()
# pyautogui.click() # pyautogui.typewrite(str(datas[key]))
# 复制粘贴效果好点
# pyperclip.copy(datas[key]) # 先复制
# pyautogui.hotkey('command', 'v') # 再粘贴
return True def main(datas, table_name):
"""
:param datas:
:param table_name: 文件名=表名
:return:
"""
auto = Auto()
x_imgs = auto.get_x_imgs(x_path=table_name)
xs = None
n = 0
while not xs:
n += 1
print(f'n:{n}')
auto.to_find_page()
xs = auto.get_xs(x_imgs=x_imgs)
auto.auto_write(xs=xs, datas=datas, x_path=table_name) if __name__ == '__main__':
datas = {'y1_2': 1}
main(datas=datas, table_name='test')

pyautogui控制鼠标键盘自动填写数据的更多相关文章

  1. Python使用pyautogui控制鼠标键盘

    官方文档:https://pyautogui.readthedocs.io/en/latest/# 安装pyautogui模块 在 Windows 上,不需要安装其他模块. 在 OS X 上,运行 s ...

  2. python,PyAutoGUI,设置鼠标键盘自动操作

    三个文件需在同一个文件夹下面,文件夹的位置无要求. 1.第一个文件,trial.py.python代码调用PyAutoGUI操作鼠标键盘,可以通过修改start_time和end_time来确定程序自 ...

  3. python如何直接控制鼠标键盘

    一.简介 我们知道在windows下输入:win + r,会弹出下面的窗口,而在下面的窗口出现后我们接着按下esc键,下面的窗口会消失 现在设想我们想在python代码里控制键盘,想通过运行代码-&g ...

  4. Python直接控制鼠标键盘

    Python直接控制鼠标键盘 之前因为期末的原因已经很久没写博客了,今天博主发现一个好玩的模块PyAutoGUI,借助它可以使用Python脚本直接控制键盘鼠标,感觉可以解决很多无聊的机械运动.这里记 ...

  5. 安利下PyAUtoGUI这个库,可自动化控制鼠标键盘

    PyAutoGUI 不知道你有没有用过,它是一款用Python自动化控制键盘.鼠标的库.但凡是你不想手动重复操作的工作都可以用这个库来解决. 比如,我想半夜时候定时给发个微信,或者每天自动刷页面等操作 ...

  6. Python——控制鼠标键盘

    一.安装包 pip install pynput 二.引用包 from pynput import mouse,keyboard 三.控制鼠标 from pynput.mouse import But ...

  7. 用代码控制鼠标键盘(C#语言)

    前些时间想做一个鼠标点击器,用到了这些知识. 下面整理记录一下. ps.感谢各位大神 下面直接上代码 1.鼠标的控制 class MouseMove { #region MouseEvent [Sys ...

  8. 【328】Python 控制鼠标/键盘+图片识别 综合应用

    本文是基于 [267]实现跨网络传数据 的基础上的,由于在弹出 putty 之后,需要手动输入命令(pass.sh.get.sh)来实现数据的传递,另外就是处理完之后需要手动关闭 putty,本文解决 ...

  9. python 监视和控制鼠标键盘的输入(使用pynput 而非pyhook)

    百度上搜到的文章大多基于pyhook, pip不能直接安装,托管在sourceForge上的代码仓库也找不到. google上发现可以使用pynput,貌似控制更为简单,而且可以直接使用pip安装 示 ...

随机推荐

  1. 【设计模式】JDK源码中用到的设计模式

    https://blog.csdn.net/angjunqiang/article/details/42061453 https://blog.csdn.net/baiye_xing/article/ ...

  2. 一次修改mysql字段类型引发的技术探究

    说来,mysql数据库是我们项目中用的比较多的库,ORM工具喜欢采用细粒度的MyBatis.这里面就这么引出了两者之间的故事! 首先,说改字段吧,将一个表中的varchar字段改为enum字段.如下: ...

  3. jQuery位置操作

    position();获取当前标签相对于最近一个父标签中有positon:relative属性的位置. height();标签纯高度 innerHeight();标签内边距padding加上纯高度 o ...

  4. oracle--分组后获取每组数据第一条数据

    SELECT * FROM (SELECT ROW_NUMBER() OVER(PARTITION BY cc.queuename ORDER BY cc.enroldate DESC) rn, cc ...

  5. Winfrom窗体无法关闭问题--检查是否存在重写

    问题描述: Winfrom窗体无法关闭问题----点击关闭/最大/最小化无法正常相应. 问题来源: 老版本的程序要求使用无边框的Form窗体(实现功能——设置为无边框窗体并重写窗体的关闭.最大.最小化 ...

  6. MFC系统自动生成的停靠窗格关掉后,如何重新显示?

    就是这几个x,关闭之后,再也显示不出来了: 原来,系统会默认记忆上次的状态,可以用函数来清除这个设置: 在MainFrame那个类中,CreateDocablePane之前,调用EnableLoadD ...

  7. 黄聪:JQUERY的datatables插件,Date range filter时间段筛选功能

    需配合moment插件实现:http://momentjs.com/ 演示:http://live.datatables.net/zuciyawi/1/edit HTML代码 <!DOCTYPE ...

  8. 在windows server 2012上安装.net3.5

    1.dism.exe /online /enable-feature /featurename:NetFX3 /Source:D:\sources\sxs /all 2.Start the Local ...

  9. Qt学习——QListWidget控件的使用

    转载:GDUTLYP Qt提供QListWidget类列表框控件用来加载并显示多个列表项.QListWidgetItem类就是列表项类. 一般列表框控件中的列表项有两种加载方式: 一种是由用户手动添加 ...

  10. [转][JS]修改链接中的参数

    转自:https://blog.csdn.net/weixin_40845192/article/details/81561644 /** * url地址修改 * @param url 待修改url ...