import hug from bottle import route, run import pyautogui import random import time import os import win32gui import autoit 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" exePath = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE" # 顶部Kpler按钮的x,y位置 kp_button_x = 920 kp_button_y = 100 # Kpler选项卡下面的table选择按钮的x,y位置 kp_table_select_button_x = 760 kp_table_select_button_y = 345 # Kpler选项卡下面的flow选择按钮的x,y位置 kp_flow_select_button_x = 760 kp_flow_select_button_y = 435 # Kpler选项卡下面的刷新按钮的x,y位置 kp_refresh_button_x = 850 kp_refresh_button_y = 300 # 获取数据的超时时间(单位:s) get_data_timeout_time = 30 # 随机点击的按钮范围 pointMinX = 200 pointMaxX = 650 pointMinY = 300 pointMaxY = 550 @hug.get('/kpler/server') def kpler_server(): return 1 @hug.get('/kpler/refresh') def kpler_refresh(FilePath): screen_width, screen_height = pyautogui.size() print("屏幕分辨率:{} x {}".format(screen_width, screen_height)) pyautogui.FAILSAFE = False FilePath = FilePath.replace('"', '') pyautogui.hotkey('win', 'd') try: fileFullPath = FilePath print("fileFullPath start") fileFullPath = fileFullPath.replace("\\\\", "\\") print(fileFullPath) 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: # 启动程序 code = exePath + " " + fileFullPath autoit.run(code) autoit.win_wait_active(file_name_ext, timeout=20) # 等待窗口激活 autoit.win_move(file_name_ext, 0, 0, 1024, 768) 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) ### 随机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(kp_button_x, kp_button_y, 0.5) pyautogui.click() time.sleep(5) 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 tableSelect") pyautogui.moveTo(kp_table_select_button_x, kp_table_select_button_y, 1) pyautogui.click() time.sleep(1) print("end tableSelect") print("start flowSelect") pyautogui.moveTo(kp_flow_select_button_x, kp_flow_select_button_y, 1) pyautogui.click() time.sleep(1) print("end flowSelect") print("start getCurrentPageData") pyautogui.moveTo(kp_refresh_button_x, kp_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 else: print("ext err:" + fileFullPath) 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=8007) # 使用不同的端口号,避免和mysteel冲突