rpa_radish_research_refresh.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import hug
  2. from bottle import run
  3. import pyautogui
  4. import random
  5. import time
  6. import os
  7. import win32gui
  8. import win32con
  9. import autoit
  10. import datetime
  11. # 配置参数
  12. CONFIG = {
  13. "exe_path": r"C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE",
  14. "no_auth_button": {"x": 1150, "y": 481},
  15. "source_button": {"x": 495, "y": 37},
  16. "refresh_icon_button": {"x": 583, "y": 112},
  17. "refresh_all_button": {"x": 616, "y": 157},
  18. "random_click_range": {"min_x": 450, "max_x": 650, "min_y": 450, "max_y": 650},
  19. "get_data_timeout_time": 20,
  20. "screen_resolution": {"width": 1920, "height": 1080}, # 默认分辨率
  21. }
  22. hug.API(__name__).http.output_format = hug.output_format.json
  23. def scale_coordinates(x, y):
  24. """根据屏幕分辨率动态缩放坐标"""
  25. screen_width, screen_height = pyautogui.size()
  26. scale_x = screen_width / CONFIG["screen_resolution"]["width"]
  27. scale_y = screen_height / CONFIG["screen_resolution"]["height"]
  28. return int(x * scale_x), int(y * scale_y)
  29. def random_click(min_x, max_x, min_y, max_y, duration=0.5):
  30. """随机点击指定范围内的坐标"""
  31. point_x = random.randint(min_x, max_x)
  32. point_y = random.randint(min_y, max_y)
  33. pyautogui.moveTo(point_x, point_y, duration)
  34. pyautogui.click()
  35. print(f"随机点击:X={point_x}, Y={point_y}")
  36. return point_x, point_y
  37. def open_excel_file(file_path):
  38. """使用 Excel 打开文件,并动态等待窗口激活"""
  39. try:
  40. file_name_ext = os.path.basename(file_path)
  41. print(f"尝试打开文件:{file_name_ext}")
  42. # 启动 Excel 并打开文件
  43. autoit.run(f'{CONFIG["exe_path"]} "{file_path}"')
  44. time.sleep(15)
  45. # 动态等待窗口激活,最多等待 20 秒
  46. # if not autoit.win_wait_active(file_name_ext, timeout=20):
  47. # print(f"窗口激活超时:{file_name_ext}")
  48. # return False
  49. # 最大化窗口
  50. hwnd = win32gui.GetForegroundWindow()
  51. win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
  52. time.sleep(1) # 短暂等待窗口最大化完成
  53. print(f"成功打开并最大化窗口:{file_name_ext}")
  54. except Exception as e:
  55. print(f"打开文件失败:{e}")
  56. return False
  57. return True
  58. @hug.get('/radish_research/server')
  59. def radish_research_server():
  60. return 1
  61. @hug.get('/radish_research/refresh')
  62. def radish_research_refresh(FilePath):
  63. pyautogui.FAILSAFE = False
  64. FilePath = FilePath.replace('"', '')
  65. # 检查文件路径
  66. if not os.path.exists(FilePath) or ".xlsx" not in FilePath \
  67. or "~$" in FilePath or "template" in FilePath:
  68. print(f"文件无效:{FilePath}")
  69. return False
  70. # 按钮坐标
  71. no_auth_button = CONFIG["no_auth_button"]["x"], CONFIG["no_auth_button"]["y"]
  72. source_button = CONFIG["source_button"]["x"], CONFIG["source_button"]["y"]
  73. refresh_icon_button = CONFIG["refresh_icon_button"]["x"], CONFIG["refresh_icon_button"]["y"]
  74. refresh_all_button = CONFIG["refresh_all_button"]["x"], CONFIG["refresh_all_button"]["y"]
  75. # 打开桌面并点击空白区域
  76. pyautogui.hotkey('win', 'd')
  77. move_x, move_y = scale_coordinates(1100, 600)
  78. pyautogui.moveTo(move_x, move_y, 0.5)
  79. pyautogui.click(move_x, move_y)
  80. try:
  81. # 打开文件
  82. if not open_excel_file(FilePath):
  83. return False
  84. # 随机点击单元格区域
  85. for _ in range(random.randint(2, 3)):
  86. random_click(
  87. CONFIG["random_click_range"]["min_x"],
  88. CONFIG["random_click_range"]["max_x"],
  89. CONFIG["random_click_range"]["min_y"],
  90. CONFIG["random_click_range"]["max_y"]
  91. )
  92. time.sleep(1)
  93. # 处理无权限弹框
  94. print("开始点击无权限按钮")
  95. pyautogui.moveTo(*no_auth_button, 0.5)
  96. pyautogui.click()
  97. time.sleep(2)
  98. print("结束点击无权限按钮")
  99. # 点击顶部萝卜投研按钮
  100. print("开始点击数据按钮")
  101. pyautogui.moveTo(*source_button, 0.5)
  102. pyautogui.click()
  103. time.sleep(2)
  104. print("结束点击数据按钮")
  105. # 点击刷新小箭头
  106. print("开始点击刷新小箭头")
  107. pyautogui.moveTo(*refresh_icon_button, 1)
  108. pyautogui.click()
  109. time.sleep(1)
  110. print("结束点击刷新小箭头")
  111. # 点击刷新所有页
  112. print("开始点击刷新所有页")
  113. pyautogui.moveTo(*refresh_all_button, 1)
  114. pyautogui.click()
  115. time.sleep(1)
  116. print("结束点击刷新所有页")
  117. # 随机点击
  118. random_click(
  119. CONFIG["random_click_range"]["min_x"],
  120. CONFIG["random_click_range"]["max_x"],
  121. CONFIG["random_click_range"]["min_y"],
  122. CONFIG["random_click_range"]["max_y"]
  123. )
  124. # 等待数据更新
  125. time.sleep(CONFIG["get_data_timeout_time"])
  126. # 保存并关闭文件
  127. pyautogui.hotkey('ctrl', 's')
  128. time.sleep(1)
  129. pyautogui.hotkey('ctrl', 'w')
  130. return True
  131. except Exception as e:
  132. print(f"发生异常:{e}")
  133. return False
  134. if __name__ == "__main__":
  135. app = __hug__.http.server()
  136. run(app=app, reloader=True, port=7007)