• pyautogui控制鼠标键盘自动填写数据


    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')
  • 相关阅读:
    [歌词]世界末日
    AJAX是什么?
    [转]Moving Your Access 2002 Database to SQL Server
    .net from身份验证的配置介绍
    ajax
    cmd常用命令
    查看端口解除端口占用
    好久没进来了,今天发一个原创的DatePicker
    真倒霉,前不久分區表錯誤把我數據全部搞沒了
    DataGrid利用DataView过滤,排序
  • 原文地址:https://www.cnblogs.com/tangpg/p/9930395.html
Copyright © 2020-2023  润新知