rpa_smm_refresh.py 5.4 KB

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