import hug from bottle import route, run import pyautogui import random import time import os import win32gui, win32con import autoit import pyperclip import pythoncom import datetime hug.API(__name__).http.output_format = hug.output_format.json exePath = r"C:\Program Files (x86)\Kingsoft\WPS Office\11.8.2.12085\office6\et" # exePath = exePath = r"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" # exePath = exePath = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE" # 顶部钢联按钮的x,y位置 gl_button_x = 645 gl_button_y = 45 # 钢联选项卡下面的刷新按钮的x,y位置 gl_refresh_button_x = 302 gl_refresh_button_y = 87 # 获取数据的超时时间(单位:s) get_data_timeout_time = 30 # 随机点击的按钮范围 pointMinX = 200 pointMaxX = 650 pointMinY = 300 pointMaxY = 550 @hug.get('/mysteel_chemical/server') def mysteel_chemical_server(): return 1 @hug.get('/mysteel_chemical/refresh') def mysteel_chemical_refresh(FilePath): screen_width, screen_height = pyautogui.size() print("屏幕分辨率:{} x {}".format(screen_width, screen_height)) pyautogui.FAILSAFE = False # FilePath = urllib.parse.unquote(FilePath) FilePath = FilePath.replace('"', '') pyautogui.hotkey('win', 'd') # time.sleep(2) # # moveX = 1100 # moveY = 600 # pyautogui.moveTo(moveX, moveY, 0.5) # pyautogui.click(moveX, moveY) try: fileFullPath = FilePath print("fileFullPath start") # print(fileFullPath) fileFullPath = fileFullPath.replace("\\\\", "\\") # fileFullPath.replace("\\", "\\") # fileFullPath.replace("ppp", "") # print("fileFullPath") print(fileFullPath) # app = xw.App(visible=True, add_book=False) # app.display_alerts = False # app.screen_updating = True # print("init app") if os.path.exists(fileFullPath) and ".xlsx" in fileFullPath \ and fileFullPath.find("~$") < 0 \ and fileFullPath.find("template") < 0: # 获取文件名和扩展名 file_name, file_ext = os.path.splitext(os.path.basename(fileFullPath)) file_name_ext = file_name + file_ext print(file_name_ext) print(datetime.datetime.now()) try: # 打开文件 # pythoncom.CoInitialize() # pyautogui.hotkey('win','r') # pyautogui.sleep(0.5) # time.sleep(2) # pyperclip.copy(fileFullPath) # pyautogui.hotkey('ctrl', 'v') # pyautogui.keyDown('enter') # pyautogui.keyUp('enter') # time.sleep(5) # # # open full screen # hwnd = win32gui.GetForegroundWindow() # win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE) # time.sleep(1) # 启动程序 code = exePath + " " + fileFullPath # print(code) autoit.run(code) autoit.win_wait_active(file_name_ext, timeout=20) # 等待窗口激活 autoit.win_move(file_name_ext, 0, 0, 1024, 768) # autoit.win_move(file_name_ext, 0, 0, 1920, 1080) print("监听成功了") except Exception as e: print("打开", fileFullPath, "失败:", e) ## 失败的话,截图记录下原因 screenshot = pyautogui.screenshot() imgPath = datetime.datetime.now().strftime("%Y%m%d%H%M%S")+"_screenshot_img.png" screenshot.save(imgPath) hwnd = win32gui.GetForegroundWindow() found_title = win32gui.GetWindowText(hwnd) print("窗口名称:", found_title) if found_title == "": print("窗口未打开") return if found_title.startswith(file_name_ext) == False: print("窗口与excel不一致,当前窗口:", found_title, ";excel:", file_name_ext) return autoit.win_move_by_handle(hwnd, 0, 0, 1024, 768) # autoit.win_move_by_handle(hwnd, 0, 0, 1920, 1080) ### 随机2-3次点击 单元格区域(每次点击停留1s) r = random.randint(2, 3) for i in range(r): pointX = random.randint(pointMinX, pointMaxX) pointY = random.randint(pointMinY, pointMaxY) print("随机点击", i + 1, "下;", "点击X:", pointX, ";Y:", pointY) pyautogui.moveTo(pointX, pointY, 0.5) pyautogui.click() time.sleep(1) print("start dataBtn") pyautogui.moveTo(gl_button_x, gl_button_y, 0.5) pyautogui.click() time.sleep(2) print("end dataBtn") pointX = random.randint(pointMinX, pointMaxX) pointY = random.randint(pointMinY, pointMaxY) pyautogui.moveTo(pointX, pointY) pyautogui.click() print("点击X:", pointX, ";Y:", pointY) # 停留随机秒数 sleepSec = random.randint(1, 3) time.sleep(sleepSec) pointX = random.randint(pointMinX, pointMaxX) pointY = random.randint(pointMinY, pointMaxY) pyautogui.moveTo(pointX, pointY) pyautogui.click() print("点击X:", pointX, ";Y:", pointY) # 停留随机秒数 sleepSec = random.randint(1, 3) time.sleep(sleepSec) print("start getCurrentPageData") pyautogui.moveTo(gl_refresh_button_x, gl_refresh_button_y, 1) pyautogui.click() time.sleep(1) print("end getCurrentPageData") pointX = random.randint(pointMinX, pointMaxX) pointY = random.randint(pointMinY, pointMaxY) pyautogui.moveTo(pointX, pointY, 0.5) pyautogui.click() print("点击X:", pointX, ";Y:", pointY) # 停留等待数据更新 time.sleep(get_data_timeout_time) pyautogui.hotkey('ctrl', 's') time.sleep(1) pyautogui.hotkey('ctrl', 'w') return True # wb.save() # # wb.close() else: print("ext err:" + fileFullPath) # app.kill() return True except Exception as e: print("Exception:") print(str(e)) return False if __name__ == "__main__": app = __hug__.http.server() run(app=app, reloader=True, port=7007)