123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- 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
- hug.API(__name__).http.output_format = hug.output_format.json
- 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 checkLoginInvalid():
- # 登录失效的弹框(类名: Button)
- pointX = 1047
- pointY = 604
- hwnd = win32gui.WindowFromPoint((pointX, pointY))
- # print("检验卓创的登录失效按钮:", hwnd)
- if hwnd != 0:
- print("检验卓创的登录失效按钮:processName:", getProcessName(hwnd), ",类名:", getClassName(hwnd))
- if getProcessName(hwnd) == "EXCEL.EXE" and getClassName(hwnd) == "Button":
- print("好吧,登录失效了")
- pyautogui.moveTo(pointX, pointY, 0.5)
- pyautogui.click()
- time.sleep(1)
- return True
- else:
- return False
- else:
- return False
- # 检验卓创的登录按钮
- def checkHongtaoLoginButton():
- # 卓创登录的弹框(类名: WindowsForms10.BUTTON.app.0.182b0e9_r93_ad2)
- pointX = 1042
- pointY = 564
- hwnd = win32gui.WindowFromPoint((pointX, pointY))
- # print("检验卓创的登录按钮:", hwnd)
- if hwnd != 0:
- className = getClassName(hwnd)
- print("检验卓创的登录按钮:processName:", getProcessName(hwnd), ",类名:", className)
- lenClassName = len(className)
- # if getProcessName(hwnd) == "EXCEL.EXE" and getClassName(hwnd) == "WindowsForms10.BUTTON.app.0.182b0e9_r93_ad2":
- if getProcessName(hwnd) == "EXCEL.EXE" and className != "Button":
- print("开始卓创登录")
- pyautogui.moveTo(pointX, pointY, 0.5)
- pyautogui.click()
- time.sleep(1)
- return True
- else:
- return False
- else:
- return False
- # 检验卓创的数据刷新组件
- def checkRefreshEnd():
- # 登录失效的弹框(类名: Button)
- # 完成了,点击结束
- pointX = 1072
- pointY = 605
- i = 1
- n = 1
- while i < 2:
- hwnd = win32gui.WindowFromPoint((pointX, pointY))
- # print("检验卓创的数据刷新组件:", hwnd)
- print("第", n, "次检测")
- n = n + 1
- if hwnd != 0:
- print("检验卓创的数据刷新组件:processName:", getProcessName(hwnd), ",类名:", getClassName(hwnd))
- if getProcessName(hwnd) == "EXCEL.EXE" and getClassName(hwnd) == "Button":
- print("ok,刷新完成了")
- pyautogui.moveTo(pointX, pointY, 0.5)
- pyautogui.click()
- time.sleep(1)
- return True
- else:
- ## 不用n=2的原因,是因为那个遮罩层可能没那么快出来,万一真实很快就被刷出来了,那么超时500s后,就超时关闭吧
- if n >= 500 and getClassName(hwnd) == "EXCEL7":
- print("ok,应该是很快就刷新完成了")
- return True
- time.sleep(1)
- else:
- return
- @hug.get('/hongtao/server')
- def mysteel_chemical_server():
- return 1
- @hug.get('/hongtao/refresh')
- def mysteel_chemical_refresh(FilePath):
- pyautogui.FAILSAFE = False
- # FilePath = urllib.parse.unquote(FilePath)
- FilePath = FilePath.replace('"', '')
- pyautogui.hotkey('win', 'd')
- # return "a"
- try:
- fileFullPath = FilePath
- print("fileFullPath start")
- print(fileFullPath)
- fileFullPath = fileFullPath.replace(r"\\", "\\")
- # fileFullPath = fileFullPath.replace("\\\\", "\\")
- # print("fileFullPath:", fileFullPath)
- if os.path.exists(fileFullPath) == False:
- return
- # 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:
- ## 鼠标移动到左下角,开始搜索文件
- # moveX = 22
- # moveY = 1058
- # pyautogui.moveTo(moveX, moveY, 0.5)
- # pyautogui.click(moveX, moveY)
- # time.sleep(2)
- # pyautogui.typewrite(fileFullPath, 0.1)
- # # print("open file:" + fileFullPath)
- # # wb = app.books.open(fileFullPath)
- # pyautogui.keyDown('enter')
- # pyautogui.keyUp('enter')
- # pyautogui.keyDown('enter')
- # pyautogui.keyUp('enter')
- # 打开文件
- pythoncom.CoInitialize()
- # openStatus = os.system("start " + fileFullPath.strip())
- openStatus = os.system('start "" "{}"'.format(fileFullPath))
- # 在上面的代码中,使用双引号将文件名引用为一个整体,这可以确保 Windows 命令行正确解释带有空格的文件路径。os.system()函数将启动一个新的命令行窗口来运行命令,然后等待命令完成。在这种情况下,"start " 命令告诉 Windows 打开文件,并使用双引号将文件名引用为一个整体。
- # print("openStatus:", openStatus)
- if openStatus == 1:
- return False
- time.sleep(3)
- ## 屏幕最大化
- # open full screen
- hwnd = win32gui.GetForegroundWindow()
- win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
- time.sleep(1)
- ### 随机2-3次点击 单元格区域
- r = random.randint(2, 3)
- for i in range(r):
- pointX = random.randint(511, 1745)
- pointY = random.randint(282, 791)
- print("随机点击", i + 1, "下")
- pyautogui.moveTo(pointX, pointY, 0.5)
- pyautogui.click()
- ## 随机停留几秒
- r = random.randint(1, 2)
- time.sleep(r)
- ##点击 红桃3 按钮
- # pointX = 641
- # pointX = 590
- pointX = 521
- pointY = 42
- pyautogui.moveTo(pointX, pointY, 0.5)
- pyautogui.click()
- time.sleep(1)
- ##点击 数据刷新 按钮
- pointX = 199
- pointY = 78
- pyautogui.moveTo(pointX, pointY, 0.5)
- pyautogui.click()
- time.sleep(1)
- ##点击 刷新工作簿 按钮
- print("开始刷新了")
- pointX = 226
- pointY = 169
- pyautogui.moveTo(pointX, pointY, 0.5)
- pyautogui.click()
- time.sleep(0.5)
- ## 校验登录失效
- checkLoginInvalid()
- ## 校验重新登录
- checkHongtaoLoginButton()
- # 点击完刷新按钮后,移开鼠标
- pyautogui.moveTo(622, 300, 0.5)
- # 等待刷新结束
- # time.sleep(15)
- checkRefreshEnd()
- # # 完成了,点击结束
- # pointX = 1072
- # pointY = 605
- # pyautogui.moveTo(pointX, pointY, 0.5)
- # pyautogui.click()
- # time.sleep(0.5)
- # 保存
- pyautogui.hotkey('ctrl', 's')
- time.sleep(1)
- # 关闭当前excel
- pyautogui.hotkey('ctrl', 'w')
- return True
- 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)
|