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. CSS 仿 iOS 系统通知数字样式

    /** 仿 iOS 系统通知数字样式 **/ .num_span{ background-color: #f00; background-image: -webkit-linear-gradient( ...

  2. golang中defer的详解 转自https://blog.csdn.net/skh2015java/article/details/77081250

    Go里的defer很有用,尤其在很多执行模块化操作时,初始化时给各个需要执行的模块传入参数,但是这些参数有些事在模块执行过程中才赋值的. 这时候有了defer就不会把代码写的很凌乱. Go的defer ...

  3. Delphi:窗体的扩展样式GWL_EXSTYLE用于SetWindowLong

    SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_TRANSPARENT or WS_EX_ ...

  4. C#中winform使用相对路径读取文件的方法

    http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f73b6cd0d3027fa3cf1fd579080101 ...

  5. hmaster 启动后自动关闭

    hbase重装后,hmaster却起不来,多次启动也不行,后来发现原因是在zookeeper中之前注册的hmaster仍然存在,系统中只允许一个hmaster运行.解决方法如下: 进入zk客户端,将h ...

  6. VSFTP再配置 我里个去马蛋网上这么多烂文章,走了好多弯路

    1.CentOS 进行yum 安装 vsftpd 2.vi /etc/vsftpd/vsftpd.conf  进行配置 3.创建FTP 用户 # useradd –d /var/www -g ftp ...

  7. 跟未名学Office - PPT操作:高效

    第二章. PPT操作:高效... 3 第一节 操作视图... 3 第二节 Word草稿... 4 第三节 幻灯片母版... 5 第四节 幻灯片页... 7 第五节 幻灯片排版... 8 第六节 模板. ...

  8. java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec 解决方案

    使用java生成图片的时候,报了java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec 错误. 根据这个类的api说明,在 ...

  9. VMWare VSphere6.0的实验笔记

    在现有的一个vsphere6.0虚拟平台上环境下搭建一套VSphere环境平台. 任务1: 1.建立1个win2008主机,192.168.12.10.16Gram,40G硬盘1独立存储+150G硬盘 ...

  10. Android WebView 开发详解

    Android WebView 开发详解 参见 http://blog.csdn.net/typename/article/details/39030091