rpa_hongtao3_refresh.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import hug
  2. from bottle import route, run
  3. import pyautogui
  4. import random
  5. import time
  6. import os
  7. import pythoncom
  8. import win32gui, win32con, win32process
  9. import psutil
  10. import urllib.parse
  11. import subprocess
  12. hug.API(__name__).http.output_format = hug.output_format.json
  13. def getProcessName(hwnd):
  14. tid, pid = win32process.GetWindowThreadProcessId(hwnd)
  15. processName = psutil.Process(pid).name()
  16. return processName
  17. def getClassName(hwnd):
  18. className = win32gui.GetClassName(hwnd)
  19. return className
  20. ## 获取窗口标题
  21. def getText(hwnd):
  22. text = win32gui.GetWindowText(hwnd)
  23. return text
  24. # 检验卓创的登录失效按钮
  25. def checkLoginInvalid():
  26. # 登录失效的弹框(类名: Button)
  27. pointX = 1047
  28. pointY = 604
  29. hwnd = win32gui.WindowFromPoint((pointX, pointY))
  30. # print("检验卓创的登录失效按钮:", hwnd)
  31. if hwnd != 0:
  32. print("检验卓创的登录失效按钮:processName:", getProcessName(hwnd), ",类名:", getClassName(hwnd))
  33. if getProcessName(hwnd) == "EXCEL.EXE" and getClassName(hwnd) == "Button":
  34. print("好吧,登录失效了")
  35. pyautogui.moveTo(pointX, pointY, 0.5)
  36. pyautogui.click()
  37. time.sleep(1)
  38. return True
  39. else:
  40. return False
  41. else:
  42. return False
  43. # 检验卓创的登录按钮
  44. def checkHongtaoLoginButton():
  45. # 卓创登录的弹框(类名: WindowsForms10.BUTTON.app.0.182b0e9_r93_ad2)
  46. pointX = 1042
  47. pointY = 564
  48. hwnd = win32gui.WindowFromPoint((pointX, pointY))
  49. # print("检验卓创的登录按钮:", hwnd)
  50. if hwnd != 0:
  51. className = getClassName(hwnd)
  52. print("检验卓创的登录按钮:processName:", getProcessName(hwnd), ",类名:", className)
  53. lenClassName = len(className)
  54. # if getProcessName(hwnd) == "EXCEL.EXE" and getClassName(hwnd) == "WindowsForms10.BUTTON.app.0.182b0e9_r93_ad2":
  55. if getProcessName(hwnd) == "EXCEL.EXE" and className != "Button":
  56. print("开始卓创登录")
  57. pyautogui.moveTo(pointX, pointY, 0.5)
  58. pyautogui.click()
  59. time.sleep(1)
  60. return True
  61. else:
  62. return False
  63. else:
  64. return False
  65. # 检验卓创的数据刷新组件
  66. def checkRefreshEnd():
  67. # 登录失效的弹框(类名: Button)
  68. # 完成了,点击结束
  69. pointX = 1072
  70. pointY = 605
  71. i = 1
  72. n = 1
  73. while i < 2:
  74. hwnd = win32gui.WindowFromPoint((pointX, pointY))
  75. # print("检验卓创的数据刷新组件:", hwnd)
  76. print("第", n, "次检测")
  77. n = n + 1
  78. if hwnd != 0:
  79. print("检验卓创的数据刷新组件:processName:", getProcessName(hwnd), ",类名:", getClassName(hwnd))
  80. if getProcessName(hwnd) == "EXCEL.EXE" and getClassName(hwnd) == "Button":
  81. print("ok,刷新完成了")
  82. pyautogui.moveTo(pointX, pointY, 0.5)
  83. pyautogui.click()
  84. time.sleep(1)
  85. return True
  86. else:
  87. ## 不用n=2的原因,是因为那个遮罩层可能没那么快出来,万一真实很快就被刷出来了,那么超时500s后,就超时关闭吧
  88. if n >= 500 and getClassName(hwnd) == "EXCEL7":
  89. print("ok,应该是很快就刷新完成了")
  90. return True
  91. time.sleep(1)
  92. else:
  93. return
  94. @hug.get('/hongtao/server')
  95. def mysteel_chemical_server():
  96. return 1
  97. @hug.get('/hongtao/refresh')
  98. def mysteel_chemical_refresh(FilePath):
  99. pyautogui.FAILSAFE = False
  100. # FilePath = urllib.parse.unquote(FilePath)
  101. FilePath = FilePath.replace('"', '')
  102. pyautogui.hotkey('win', 'd')
  103. # return "a"
  104. try:
  105. fileFullPath = FilePath
  106. print("fileFullPath start")
  107. print(fileFullPath)
  108. fileFullPath = fileFullPath.replace(r"\\", "\\")
  109. # fileFullPath = fileFullPath.replace("\\\\", "\\")
  110. # print("fileFullPath:", fileFullPath)
  111. if os.path.exists(fileFullPath) == False:
  112. return
  113. # app = xw.App(visible=True, add_book=False)
  114. # app.display_alerts = False
  115. # app.screen_updating = True
  116. # print("init app")
  117. if os.path.exists(fileFullPath) and ".xlsx" in fileFullPath \
  118. and fileFullPath.find("~$") < 0 \
  119. and fileFullPath.find("template") < 0:
  120. ## 鼠标移动到左下角,开始搜索文件
  121. # moveX = 22
  122. # moveY = 1058
  123. # pyautogui.moveTo(moveX, moveY, 0.5)
  124. # pyautogui.click(moveX, moveY)
  125. # time.sleep(2)
  126. # pyautogui.typewrite(fileFullPath, 0.1)
  127. # # print("open file:" + fileFullPath)
  128. # # wb = app.books.open(fileFullPath)
  129. # pyautogui.keyDown('enter')
  130. # pyautogui.keyUp('enter')
  131. # pyautogui.keyDown('enter')
  132. # pyautogui.keyUp('enter')
  133. # 打开文件
  134. pythoncom.CoInitialize()
  135. # openStatus = os.system("start " + fileFullPath.strip())
  136. openStatus = os.system('start "" "{}"'.format(fileFullPath))
  137. # 在上面的代码中,使用双引号将文件名引用为一个整体,这可以确保 Windows 命令行正确解释带有空格的文件路径。os.system()函数将启动一个新的命令行窗口来运行命令,然后等待命令完成。在这种情况下,"start " 命令告诉 Windows 打开文件,并使用双引号将文件名引用为一个整体。
  138. # print("openStatus:", openStatus)
  139. if openStatus == 1:
  140. return False
  141. time.sleep(3)
  142. ## 屏幕最大化
  143. # open full screen
  144. hwnd = win32gui.GetForegroundWindow()
  145. win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
  146. time.sleep(1)
  147. ### 随机2-3次点击 单元格区域
  148. r = random.randint(2, 3)
  149. for i in range(r):
  150. pointX = random.randint(511, 1745)
  151. pointY = random.randint(282, 791)
  152. print("随机点击", i + 1, "下")
  153. pyautogui.moveTo(pointX, pointY, 0.5)
  154. pyautogui.click()
  155. ## 随机停留几秒
  156. r = random.randint(1, 2)
  157. time.sleep(r)
  158. ##点击 红桃3 按钮
  159. # pointX = 641
  160. # pointX = 590
  161. pointX = 521
  162. pointY = 42
  163. pyautogui.moveTo(pointX, pointY, 0.5)
  164. pyautogui.click()
  165. time.sleep(1)
  166. ##点击 数据刷新 按钮
  167. pointX = 199
  168. pointY = 78
  169. pyautogui.moveTo(pointX, pointY, 0.5)
  170. pyautogui.click()
  171. time.sleep(1)
  172. ##点击 刷新工作簿 按钮
  173. print("开始刷新了")
  174. pointX = 226
  175. pointY = 169
  176. pyautogui.moveTo(pointX, pointY, 0.5)
  177. pyautogui.click()
  178. time.sleep(0.5)
  179. ## 校验登录失效
  180. checkLoginInvalid()
  181. ## 校验重新登录
  182. checkHongtaoLoginButton()
  183. # 点击完刷新按钮后,移开鼠标
  184. pyautogui.moveTo(622, 300, 0.5)
  185. # 等待刷新结束
  186. # time.sleep(15)
  187. checkRefreshEnd()
  188. # # 完成了,点击结束
  189. # pointX = 1072
  190. # pointY = 605
  191. # pyautogui.moveTo(pointX, pointY, 0.5)
  192. # pyautogui.click()
  193. # time.sleep(0.5)
  194. # 保存
  195. pyautogui.hotkey('ctrl', 's')
  196. time.sleep(1)
  197. # 关闭当前excel
  198. pyautogui.hotkey('ctrl', 'w')
  199. return True
  200. else:
  201. print("ext err:" + fileFullPath)
  202. # app.kill()
  203. return True
  204. except Exception as e:
  205. print("Exception:")
  206. print(str(e))
  207. return False
  208. if __name__ == "__main__":
  209. app = __hug__.http.server()
  210. run(app=app, reloader=True, port=7007)