rpa_smm_refresh.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. @hug.get('/smm_chemical/refresh')
  39. def smm_refresh(file_path):
  40. screen_width, screen_height = pyautogui.size()
  41. print("当前时间:{};当前屏幕分辨率:{} x {}".format(datetime.datetime.now(), screen_width, screen_height))
  42. pyautogui.FAILSAFE = False
  43. file_path = file_path.replace('"', '')
  44. # 显示桌面
  45. pyautogui.hotkey('win', 'd')
  46. try:
  47. file_path = urllib.parse.unquote(file_path)
  48. fileFullPath = file_path
  49. # print("full_path start")
  50. fileFullPath = fileFullPath.replace("\\\\", "\\")
  51. print("文件路径:", fileFullPath)
  52. if not os.path.exists(fileFullPath):
  53. print("文件不存在")
  54. return
  55. if os.path.exists(fileFullPath) and ".xlsx" in fileFullPath \
  56. and fileFullPath.find("~$") < 0 \
  57. and fileFullPath.find("template") < 0:
  58. # 获取文件名和扩展名
  59. file_name, file_ext = os.path.splitext(os.path.basename(fileFullPath))
  60. file_name_ext = file_name + file_ext
  61. # print(file_name_ext)
  62. try:
  63. # 启动程序
  64. code = exePath + " " + fileFullPath
  65. # print(code)
  66. autoit.run(code)
  67. autoit.win_wait_active(file_name_ext, timeout=20) # 等待窗口激活
  68. autoit.win_move(file_name_ext, 0, 0, 1024, 768)
  69. # autoit.win_move(file_name_ext, 0, 0, 1920, 1080)
  70. # print("监听成功了")
  71. except Exception as e:
  72. print("监听", fileFullPath, "失败:", e)
  73. ## 失败的话,截图记录下原因
  74. screenshot = pyautogui.screenshot()
  75. imgPath = datetime.datetime.now().strftime("%Y%m%d%H%M%S")+"_screenshot_img.png"
  76. screenshot.save(imgPath)
  77. hwnd = win32gui.GetForegroundWindow()
  78. found_title = win32gui.GetWindowText(hwnd)
  79. print("当前窗口名称:", found_title)
  80. if found_title == "":
  81. print("窗口未打开")
  82. return
  83. if found_title.startswith(file_name_ext) == False:
  84. print("当前窗口与excel不一致,当前窗口:", found_title, ";excel:", file_name_ext)
  85. return
  86. autoit.win_move_by_handle(hwnd, 0, 0, 1024, 768)
  87. # autoit.win_move_by_handle(hwnd, 0, 0, 1920, 1080)
  88. # 等待自动刷新结束
  89. #time.sleep(15)
  90. # 保存
  91. #pyautogui.hotkey('ctrl', 's')
  92. #time.sleep(1)
  93. # 关闭当前excel
  94. #pyautogui.hotkey('ctrl', 'w')
  95. #return True
  96. # 屏幕最大化
  97. hwnd = win32gui.GetForegroundWindow()
  98. win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
  99. time.sleep(1)
  100. # 随机2-3次点击 单元格区域
  101. r = random.randint(2, 3)
  102. for i in range(r):
  103. pointX = random.randint(pointMinX, pointMaxX)
  104. pointY = random.randint(pointMinY, pointMaxY)
  105. print("随机点击", i + 1, "下;", "点击X:", pointX, ";Y:", pointY)
  106. pyautogui.moveTo(pointX, pointY, 0.5)
  107. pyautogui.click()
  108. time.sleep(1)
  109. # 随机停留几秒
  110. r = random.randint(1, 2)
  111. time.sleep(r)
  112. # 点击 SMM DATA PRO 按钮
  113. # point_x = 541
  114. # point_y = 63
  115. pyautogui.moveTo(smm_button_x, smm_button_y, 0.5)
  116. pyautogui.click()
  117. time.sleep(1)
  118. # 点击 刷新下拉框 按钮
  119. # point_x = 210
  120. # point_y = 142
  121. pyautogui.moveTo(smm_refresh_select_button_x, smm_refresh_select_button_y, 0.5)
  122. pyautogui.click()
  123. time.sleep(1)
  124. # 点击 全部工作表 按钮
  125. print("开始刷新了")
  126. # point_x = 240
  127. # point_y = 233
  128. pyautogui.moveTo(smm_refresh_button_x, smm_refresh_button_y, 0.5)
  129. pyautogui.click()
  130. time.sleep(0.5)
  131. pointX = random.randint(pointMinX, pointMaxX)
  132. pointY = random.randint(pointMinY, pointMaxY)
  133. # 点击完刷新按钮后,移开鼠标
  134. pyautogui.moveTo(pointX, pointY, 0.5)
  135. # 等待刷新结束
  136. time.sleep(timeout_time)
  137. # 保存
  138. pyautogui.hotkey('ctrl', 's')
  139. time.sleep(1)
  140. # 关闭当前excel
  141. pyautogui.hotkey('ctrl', 'w')
  142. return True
  143. else:
  144. print("ext err:" + fileFullPath)
  145. return True
  146. except Exception as e:
  147. print("Exception:")
  148. print(str(e))
  149. return False
  150. if __name__ == "__main__":
  151. app = __hug__.http.server()
  152. run(app=app, reloader=True, port=7007)