123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- import hug
- from bottle import route, run
- import pyautogui
- import random
- import time
- import os
- import pythoncom
- import win32gui, win32con, win32process
- import psutil
- import urllib.parse
- import subprocess
- import autoit
- import datetime
- hug.API(__name__).http.output_format = hug.output_format.json
- exePath = r"C:\Program Files\Microsoft Office\Office16\EXCEL.EXE"
- hongTao3PointX = 497
- hongTao3PointY = 47
- refreshButtonPointX = 247
- refreshButtonPointY = 102
- refreshSheetPointX = 274
- refreshSheetPointY = 174
- loginButtonPointX = 518
- loginButtonPointY = 437
- refreshEndPointX = 747
- refreshEndPointY = 451
- minPointX = 511
- maxPointX = 1000
- minPointY = 282
- maxPointY = 600
- excelProcessName = "EXCEL.EXE"
- hongTao3LoginButtonName = "WindowsForms10.Window.8.app.0.182b0e9_r55_ad2"
- refreshIngButtonName = "WindowsForms10.Window.8.app.0.182b0e9_r57_ad2"
- refreshEndButtonName = "#32770"
- def getProcessName(hwnd):
- tid, pid = win32process.GetWindowThreadProcessId(hwnd)
- processName = psutil.Process(pid).name()
- return processName
- def getClassName(hwnd):
- className = win32gui.GetClassName(hwnd)
- return className
- def getText(hwnd):
- text = win32gui.GetWindowText(hwnd)
- return text
- def checkHongtaoLoginButton():
-
-
- hwnd = win32gui.GetForegroundWindow()
-
- if hwnd != 0:
- className = getClassName(hwnd)
- print("检验卓创的登录按钮:processName:", getProcessName(hwnd), ",类名:", className)
- lenClassName = len(className)
-
- if getProcessName(hwnd) == excelProcessName and className == hongTao3LoginButtonName:
- print("开始卓创登录")
- pyautogui.moveTo(loginButtonPointX, loginButtonPointY, 0.5)
- pyautogui.click()
- time.sleep(1)
- return True
- else:
- return False
- else:
- return False
- def checkRefreshEnd():
-
-
-
- time.sleep(2)
- i = 1
- n = 1
-
- hwndN = 0
- while i < 2:
-
- hwnd = win32gui.GetForegroundWindow()
-
- print("第", n, "次检测")
- n = n + 1
- print("hwnd:", hwnd)
- if hwnd != 0:
-
- hwndN = 0
- print("检验卓创的数据刷新组件:processName:", getProcessName(hwnd), ",类名:", getClassName(hwnd))
- if getProcessName(hwnd) == excelProcessName and getClassName(hwnd) == refreshEndButtonName:
- print("ok,刷新完成了")
- pyautogui.moveTo(refreshEndPointX, refreshEndPointY, 0.5)
- pyautogui.click()
- time.sleep(1)
- return True
-
- if n > 500:
- print("超时500s,默认执行完成吧")
- return True
- if getProcessName(hwnd) != excelProcessName:
- print("目前活动窗口与实际不符,终止执行")
- return False
-
- time.sleep(1)
- else:
-
- if hwndN > 5:
- return False
- hwndN = hwndN + 1
-
- time.sleep(2)
- @hug.get('/hongtao/server')
- def mysteel_chemical_server():
- return 1
- @hug.get('/hongtao/refresh')
- def mysteel_chemical_refresh(FilePath):
- pyautogui.FAILSAFE = False
- file_path = FilePath
- print(FilePath)
- screen_width, screen_height = pyautogui.size()
- print("当前时间:{};当前屏幕分辨率:{} x {}".format(datetime.datetime.now(), screen_width, screen_height))
- pyautogui.FAILSAFE = False
- file_path = file_path.replace('"', '')
-
- pyautogui.hotkey('win', 'd')
-
- try:
- file_path = urllib.parse.unquote(file_path)
- fileFullPath = file_path
-
- fileFullPath = fileFullPath.replace("\\\\", "\\")
- print("文件路径:", fileFullPath)
- if not os.path.exists(fileFullPath):
- print("文件不存在")
- return
-
-
-
-
- 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
- time.sleep(3)
- 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)
-
-
- 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
-
- for err_windows_name in err_windows_name_list:
- if found_title.startswith(err_windows_name):
-
- process_id = win32process.GetWindowThreadProcessId(hwnd)[1]
-
- target_process = psutil.Process(process_id)
- target_process.terminate()
- print("可能是EXCEL崩溃导致,优雅退出EXCEL")
- 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)
-
-
-
-
-
-
- time.sleep(1)
-
- r = random.randint(2, 3)
- for i in range(r):
- pointX = random.randint(minPointX, maxPointX)
- pointY = random.randint(minPointY, maxPointY)
- print("随机点击", i + 1, "下")
- pyautogui.moveTo(pointX, pointY, 0.5)
- pyautogui.click()
-
- r = random.randint(1, 2)
- time.sleep(r)
-
- pyautogui.moveTo(hongTao3PointX, hongTao3PointY, 0.5)
- pyautogui.click()
- time.sleep(1)
-
- pyautogui.moveTo(refreshButtonPointX, refreshButtonPointY, 0.5)
- pyautogui.click()
- time.sleep(1)
-
- print("开始刷新了")
- pyautogui.moveTo(refreshSheetPointX, refreshSheetPointY, 0.5)
- pyautogui.click()
- time.sleep(0.5)
-
-
-
- checkHongtaoLoginButton()
-
- pyautogui.moveTo(622, 300, 0.5)
-
-
- checkRefreshEnd()
-
-
-
-
-
-
-
- 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, host="0.0.0.0", reloader=True, port=7007, server="waitress")
|