rpa_smm_refresh.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 pyperclip
  10. import psutil
  11. import urllib.parse
  12. import autoit
  13. import datetime
  14. # import subprocess
  15. hug.API(__name__).http.output_format = hug.output_format.json
  16. exePath = r"C:\Program Files (x86)\Kingsoft\WPS Office\11.8.2.12085\office6\et"
  17. # exePath = exePath = r"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"
  18. # exePath = exePath = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
  19. @hug.get('/smm_chemical/server')
  20. def smm_server():
  21. return 1
  22. # 顶部有色按钮的x,y位置
  23. smm_button_x = 642
  24. smm_button_y = 53
  25. # 中间更新数据下拉框按钮的x,y位置
  26. smm_refresh_select_button_x = 212
  27. smm_refresh_select_button_y = 119
  28. # 中间更新数据按钮的x,y位置
  29. smm_refresh_button_x = 232
  30. smm_refresh_button_y = 178
  31. # 超时时间,默认15s
  32. timeout_time = 15
  33. # 随机点击的按钮范围
  34. pointMinX = 200
  35. pointMaxX = 650
  36. pointMinY = 300
  37. pointMaxY = 550
  38. # 异常的监控弹框,需要关闭进程的
  39. err_windows_name_list = ["WPS Office","另存为"]
  40. @hug.get('/smm_chemical/refresh')
  41. def smm_refresh(file_path):
  42. screen_width, screen_height = pyautogui.size()
  43. print("当前时间:{};当前屏幕分辨率:{} x {}".format(datetime.datetime.now(), screen_width, screen_height))
  44. pyautogui.FAILSAFE = False
  45. file_path = file_path.replace('"', '')
  46. # 显示桌面
  47. pyautogui.hotkey('win', 'd')
  48. try:
  49. file_path = urllib.parse.unquote(file_path)
  50. fileFullPath = file_path
  51. # print("full_path start")
  52. fileFullPath = fileFullPath.replace("\\\\", "\\")
  53. print("文件路径:", fileFullPath)
  54. if not os.path.exists(fileFullPath):
  55. print("文件不存在")
  56. return
  57. if os.path.exists(fileFullPath) and ".xlsx" in fileFullPath \
  58. and fileFullPath.find("~$") < 0 \
  59. and fileFullPath.find("template") < 0:
  60. # 获取文件名和扩展名
  61. file_name, file_ext = os.path.splitext(os.path.basename(fileFullPath))
  62. file_name_ext = file_name + file_ext
  63. # print(file_name_ext)
  64. # 停止2s打开,避免打开过快导致excel崩溃
  65. time.sleep(2)
  66. try:
  67. # 启动程序
  68. code = exePath + " " + fileFullPath
  69. # print(code)
  70. autoit.run(code)
  71. autoit.win_wait_active(file_name_ext, timeout=20) # 等待窗口激活
  72. autoit.win_move(file_name_ext, 0, 0, 1024, 768)
  73. # autoit.win_move(file_name_ext, 0, 0, 1920, 1080)
  74. # print("监听成功了")
  75. except Exception as e:
  76. print("监听", fileFullPath, "失败:", e)
  77. ## 失败的话,截图记录下原因
  78. screenshot = pyautogui.screenshot()
  79. imgPath = datetime.datetime.now().strftime("%Y%m%d%H%M%S")+"_screenshot_img.png"
  80. screenshot.save(imgPath)
  81. hwnd = win32gui.GetForegroundWindow()
  82. found_title = win32gui.GetWindowText(hwnd)
  83. print("当前窗口名称:", found_title)
  84. if found_title == "":
  85. print("窗口未打开")
  86. return
  87. # 遍历异常名称列表去校验,如果匹配上了,那么就要退出excel并打开
  88. for err_windows_name in err_windows_name_list:
  89. if found_title.startswith(err_windows_name):
  90. # 获取与窗口句柄关联的进程ID
  91. process_id = win32process.GetWindowThreadProcessId(hwnd)[1]
  92. # 根据进程ID获取进程对象并尝试优雅地终止进程
  93. target_process = psutil.Process(process_id)
  94. target_process.terminate()
  95. print("可能是EXCEL崩溃导致,优雅退出EXCEL")
  96. return
  97. # 如果是office的,那么就是要将file_name_ext变更为 file_name;因为office没有后缀
  98. if found_title.startswith(file_name_ext) == False:
  99. print("当前窗口与excel不一致,当前窗口:", found_title, ";excel:", file_name_ext)
  100. return
  101. autoit.win_move_by_handle(hwnd, 0, 0, 1024, 768)
  102. # autoit.win_move_by_handle(hwnd, 0, 0, 1920, 1080)
  103. # 等待自动刷新结束
  104. #time.sleep(15)
  105. # 保存
  106. #pyautogui.hotkey('ctrl', 's')
  107. #time.sleep(1)
  108. # 关闭当前excel
  109. #pyautogui.hotkey('ctrl', 'w')
  110. #return True
  111. # 屏幕最大化
  112. #hwnd = win32gui.GetForegroundWindow()
  113. #win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
  114. #time.sleep(1)
  115. # 随机2-3次点击 单元格区域
  116. r = random.randint(2, 3)
  117. for i in range(r):
  118. pointX = random.randint(pointMinX, pointMaxX)
  119. pointY = random.randint(pointMinY, pointMaxY)
  120. print("随机点击", i + 1, "下;", "点击X:", pointX, ";Y:", pointY)
  121. pyautogui.moveTo(pointX, pointY, 0.5)
  122. pyautogui.click()
  123. time.sleep(1)
  124. # 随机停留几秒
  125. r = random.randint(1, 2)
  126. time.sleep(r)
  127. # 点击 SMM DATA PRO 按钮
  128. # point_x = 541
  129. # point_y = 63
  130. pyautogui.moveTo(smm_button_x, smm_button_y, 0.5)
  131. pyautogui.click()
  132. time.sleep(1)
  133. # 点击 刷新下拉框 按钮
  134. # point_x = 210
  135. # point_y = 142
  136. pyautogui.moveTo(smm_refresh_select_button_x, smm_refresh_select_button_y, 0.5)
  137. pyautogui.click()
  138. time.sleep(1)
  139. # 点击 全部工作表 按钮
  140. print("开始刷新了")
  141. # point_x = 240
  142. # point_y = 233
  143. pyautogui.moveTo(smm_refresh_button_x, smm_refresh_button_y, 0.5)
  144. pyautogui.click()
  145. time.sleep(0.5)
  146. pointX = random.randint(pointMinX, pointMaxX)
  147. pointY = random.randint(pointMinY, pointMaxY)
  148. # 点击完刷新按钮后,移开鼠标
  149. pyautogui.moveTo(pointX, pointY, 0.5)
  150. # 等待刷新结束
  151. time.sleep(timeout_time)
  152. # 保存
  153. pyautogui.hotkey('ctrl', 's')
  154. time.sleep(1)
  155. # 关闭当前excel
  156. pyautogui.hotkey('ctrl', 'w')
  157. return True
  158. else:
  159. print("ext err:" + fileFullPath)
  160. return True
  161. except Exception as e:
  162. print("Exception:")
  163. print(str(e))
  164. return False
  165. if __name__ == "__main__":
  166. app = __hug__.http.server()
  167. run(app=app, reloader=True, port=7007)