rpa_baiinfo_refresh.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import hug
  2. from bottle import route, run
  3. import pyautogui
  4. import random
  5. import time
  6. import os
  7. import win32gui, win32con
  8. import autoit
  9. import pyperclip
  10. import pythoncom
  11. import datetime
  12. hug.API(__name__).http.output_format = hug.output_format.json
  13. exePath = r"C:\Program Files (x86)\Kingsoft\WPS Office\11.8.2.12085\office6\et"
  14. # exePath = exePath = r"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"
  15. # exePath = exePath = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
  16. # 顶部百川按钮的x,y位置
  17. top_refresh_button_x = 645
  18. top_refresh_button_y = 45
  19. # 百川选项卡下面的刷新按钮的x,y位置
  20. real_refresh_button_x = 302
  21. real_refresh_button_y = 87
  22. # 获取数据的超时时间(单位:s)
  23. get_data_timeout_time = 30
  24. # 随机点击的按钮范围
  25. pointMinX = 200
  26. pointMaxX = 650
  27. pointMinY = 300
  28. pointMaxY = 550
  29. @hug.get('/baiinfo/server')
  30. def baiinfo_server():
  31. return 1
  32. @hug.get('/baiinfo/refresh')
  33. def baiinfo_refresh(file_path):
  34. screen_width, screen_height = pyautogui.size()
  35. print("屏幕分辨率:{} x {}".format(screen_width, screen_height))
  36. pyautogui.FAILSAFE = False
  37. # file_path = urllib.parse.unquote(file_path)
  38. file_path = file_path.replace('"', '')
  39. pyautogui.hotkey('win', 'd')
  40. # time.sleep(2)
  41. #
  42. # moveX = 1100
  43. # moveY = 600
  44. # pyautogui.moveTo(moveX, moveY, 0.5)
  45. # pyautogui.click(moveX, moveY)
  46. try:
  47. fileFullPath = file_path
  48. print("fileFullPath start")
  49. # print(fileFullPath)
  50. fileFullPath = fileFullPath.replace("\\\\", "\\")
  51. # fileFullPath.replace("\\", "\\")
  52. # fileFullPath.replace("ppp", "")
  53. # print("fileFullPath")
  54. print(fileFullPath)
  55. # app = xw.App(visible=True, add_book=False)
  56. # app.display_alerts = False
  57. # app.screen_updating = True
  58. # print("init app")
  59. if os.path.exists(fileFullPath) and ".xlsx" in fileFullPath \
  60. and fileFullPath.find("~$") < 0 \
  61. and fileFullPath.find("template") < 0:
  62. # 获取文件名和扩展名
  63. file_name, file_ext = os.path.splitext(os.path.basename(fileFullPath))
  64. file_name_ext = file_name + file_ext
  65. print(file_name_ext)
  66. print(datetime.datetime.now())
  67. try:
  68. # 打开文件
  69. # pythoncom.CoInitialize()
  70. # pyautogui.hotkey('win','r')
  71. # pyautogui.sleep(0.5)
  72. # time.sleep(2)
  73. # pyperclip.copy(fileFullPath)
  74. # pyautogui.hotkey('ctrl', 'v')
  75. # pyautogui.keyDown('enter')
  76. # pyautogui.keyUp('enter')
  77. # time.sleep(5)
  78. #
  79. # # open full screen
  80. # hwnd = win32gui.GetForegroundWindow()
  81. # win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
  82. # time.sleep(1)
  83. # 启动程序
  84. code = exePath + " " + fileFullPath
  85. # print(code)
  86. autoit.run(code)
  87. autoit.win_wait_active(file_name_ext, timeout=20) # 等待窗口激活
  88. autoit.win_move(file_name_ext, 0, 0, 1024, 768)
  89. # autoit.win_move(file_name_ext, 0, 0, 1920, 1080)
  90. print("监听成功了")
  91. except Exception as e:
  92. print("打开", fileFullPath, "失败:", e)
  93. ## 失败的话,截图记录下原因
  94. screenshot = pyautogui.screenshot()
  95. imgPath = datetime.datetime.now().strftime("%Y%m%d%H%M%S") + "_screenshot_img.png"
  96. screenshot.save(imgPath)
  97. hwnd = win32gui.GetForegroundWindow()
  98. found_title = win32gui.GetWindowText(hwnd)
  99. print("窗口名称:", found_title)
  100. if found_title == "":
  101. print("窗口未打开")
  102. return
  103. if found_title.startswith(file_name_ext) == False:
  104. print("窗口与excel不一致,当前窗口:", found_title, ";excel:", file_name_ext)
  105. return
  106. autoit.win_move_by_handle(hwnd, 0, 0, 1024, 768)
  107. # autoit.win_move_by_handle(hwnd, 0, 0, 1920, 1080)
  108. ### 随机2-3次点击 单元格区域(每次点击停留1s)
  109. r = random.randint(2, 3)
  110. for i in range(r):
  111. pointX = random.randint(pointMinX, pointMaxX)
  112. pointY = random.randint(pointMinY, pointMaxY)
  113. print("随机点击", i + 1, "下;", "点击X:", pointX, ";Y:", pointY)
  114. pyautogui.moveTo(pointX, pointY, 0.5)
  115. pyautogui.click()
  116. time.sleep(1)
  117. print("start dataBtn")
  118. pyautogui.moveTo(top_refresh_button_x, top_refresh_button_y, 0.5)
  119. pyautogui.click()
  120. time.sleep(2)
  121. print("end dataBtn")
  122. pointX = random.randint(pointMinX, pointMaxX)
  123. pointY = random.randint(pointMinY, pointMaxY)
  124. pyautogui.moveTo(pointX, pointY)
  125. pyautogui.click()
  126. print("点击X:", pointX, ";Y:", pointY)
  127. # 停留随机秒数
  128. sleepSec = random.randint(1, 3)
  129. time.sleep(sleepSec)
  130. pointX = random.randint(pointMinX, pointMaxX)
  131. pointY = random.randint(pointMinY, pointMaxY)
  132. pyautogui.moveTo(pointX, pointY)
  133. pyautogui.click()
  134. print("点击X:", pointX, ";Y:", pointY)
  135. # 停留随机秒数
  136. sleepSec = random.randint(1, 3)
  137. time.sleep(sleepSec)
  138. print("start getCurrentPageData")
  139. pyautogui.moveTo(real_refresh_button_x, real_refresh_button_y, 1)
  140. pyautogui.click()
  141. time.sleep(1)
  142. print("end getCurrentPageData")
  143. pointX = random.randint(pointMinX, pointMaxX)
  144. pointY = random.randint(pointMinY, pointMaxY)
  145. pyautogui.moveTo(pointX, pointY, 0.5)
  146. pyautogui.click()
  147. print("点击X:", pointX, ";Y:", pointY)
  148. # 停留等待数据更新
  149. time.sleep(get_data_timeout_time)
  150. pyautogui.hotkey('ctrl', 's')
  151. time.sleep(1)
  152. pyautogui.hotkey('ctrl', 'w')
  153. return True
  154. # wb.save()
  155. #
  156. # wb.close()
  157. else:
  158. print("ext err:" + fileFullPath)
  159. # app.kill()
  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, host='0.0.0.0', port=7007)