python自動化常用操作


python自動化常用操作

import pyautogui
import webbrowser as web
from time import sleep
import os

# from openpyxl import Workbook
# 獲取當前屏幕分辨率
screenWidth, screenHeight = pyautogui.size()
# 03405197
currentMouseX, currentMouseY = pyautogui.position()
# pyautogui.FAILSAF123456aB-E = True
# pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.01

# confidence = 0.9: 像素匹配度


class Robot():
    def __init__(self):
        pass

    # 用IE瀏覽器打開指定網址
    def ie_open_url(self, url):
        browser_path = r'C:\Program Files (x86)\Internet Explorer\iexplore.exe'
        web.register('IE', None, web.BackgroundBrowser(browser_path))
        web.get('IE').open_new_tab(url)
        print('use_IE_open_url  open url ending ....')

    # 打開exe
    def open_exe(self, path):
        os.startfile(path)

    # 瀏覽器最大化
    def ie_max(self,path):
        try:
            self.img_click(path)
        except:
            print('已經最大化')

    # 關閉exe
    def close_exe(self, exe_name):
        """
        :param exe_name: os.system("taskkill /F /IM iexplore.exe")
        :return:
        """
        os.system("taskkill /F /IM {}".format(exe_name))

    # 關閉瀏覽器
    def ie_close_url(self, path):
        try:
            # self.img_click('D:/img_rb/關閉瀏覽器.png')
            self.img_click(path)
            print('瀏覽器關閉成功')
        except:
            print('瀏覽器關閉失敗')

    # 最大化
    def ie_big(self, path):
        try:
            # self.img_click('D:/img_rb/最大化瀏覽器.png')
            self.img_click(path)
        except:
            print('最大化瀏覽器失敗')

    # ###############################################

    # 輸入文字
    def type_text(self, text):
        pyautogui.typewrite(text)

    # 根據截圖點擊
    def img_click(self, img_path, num=1):
        # #在當前屏幕中查找指定圖片(圖片需要由系統截圖功能截取的圖)
        coords = pyautogui.locateOnScreen(img_path)
        # confidence = 0.9: 像素匹配度

        # #獲取定位到的圖中間點坐標
        x, y = pyautogui.center(coords)
        pyautogui.click(x=x, y=y, clicks=num, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)

    # 創建文件夾
    def crate_dir(self, path):
        if not os.path.exists(path):
            os.mkdir(path)
        else:
            print('此文件夾已經存在')

    # 檢測圖片是否存在
    def img_check(self, img_path, t_all=100, t1=0.01):
        num = 0
        while num <= t_all:
            num += 1
            print(num)
            sleep(t1)
            try:
                # #在當前屏幕中查找指定圖片(圖片需要由系統截圖功能截取的圖)
                coords = pyautogui.locateOnScreen(img_path)
                print(coords)
                if coords:
                    break
                else:
                    continue

            except:
                print('圖片檢測失敗')
                return False

        # # #在當前屏幕中查找指定圖片(圖片需要由系統截圖功能截取的圖)
        # coords = pyautogui.locateOnScreen(img_path)
        # print(coords)
        # # #獲取定位到的圖中間點坐標
        # x, y = pyautogui.center(coords)
        # print(x, y)
        # pyautogui.click(x=x, y=y, clicks=1, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)
        # return 1

    # 向下移動滑塊
    def move_down(self, path, x=0, y=0):
        self.img_click(path)
        sleep(2)
        pyautogui.dragRel(x, y)
        sleep(2)

    # 重命名文件夾
    def re_name(self, data_name, new_name):
        """
        :param data_name: data_name = "CP_費用報銷明細表_" + day + month + year + ".xls"
        :param new_name: new_name = "666666666666666666666666666666666666666.xls"
        :return: True
        """
        import os
        import datetime
        new_time = datetime.datetime.now()
        year = str(new_time).split('-')[0][2:4]
        month = str(new_time).split('-')[1]
        day = str(new_time).split('-')[2][:2]
        data_name_new = data_name + day + month + year + ".xls"
        try:
            os.remove(f'C:/Users/Administrator/Desktop/{new_name}')
        except:
            print('不存在')
        os.rename(f'C:/Users/Administrator/Desktop/{data_name_new}', f'C:/Users/Administrator/Desktop/{new_name}')

    # 鼠標滑塊移動
    def rolling(self):
        pyautogui.scroll(100, x=100, y=100)

    # 截圖
    def screen(self, x1, y1, x2, y2, path):
        """
        :param x1: 左側,
        :param y1: 頂部,
        :param x2: 寬度
        :param y2: 高度
        :param path: 保存路徑
        :return:
        """
        im = pyautogui.screenshot(region=(0, 0, 300, 400))
        im.save(path)

    # 驗證碼識別
    def img_dis(self, path):
        import pytesseract
        from PIL import Image

        image = Image.open(path)

        code = pytesseract.image_to_string(image)
        print(code)


    # 鍵盤操作
    def jianpan(self):
        pyautogui.press('esc')
        # 按住shift鍵
        pyautogui.keyDown('shift')
        # 放開shift鍵
        pyautogui.keyUp('shift')
        # 模擬組合熱鍵
        pyautogui.hotkey('ctrl', 'c')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM