rpa_mysteel_refresh.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. # 顶部钢联按钮的x,y位置
  15. gl_button_x = 645
  16. gl_button_y = 45
  17. # 钢联选项卡下面的刷新按钮的x,y位置
  18. gl_refresh_button_x = 302
  19. gl_refresh_button_y = 87
  20. # 获取数据的超时时间(单位:s)
  21. get_data_timeout_time = 30
  22. # 随机点击的按钮范围
  23. pointMinX = 200
  24. pointMaxX = 700
  25. pointMinY = 300
  26. pointMaxY = 600
  27. @hug.get('/mysteel_chemical/server')
  28. def mysteel_chemical_server():
  29. return 1
  30. @hug.get('/mysteel_chemical/refresh')
  31. def mysteel_chemical_refresh(FilePath):
  32. screen_width, screen_height = pyautogui.size()
  33. print("屏幕分辨率:{} x {}".format(screen_width, screen_height))
  34. pyautogui.FAILSAFE = False
  35. # FilePath = urllib.parse.unquote(FilePath)
  36. FilePath = FilePath.replace('"', '')
  37. # pyautogui.hotkey('win', 'd')
  38. # time.sleep(2)
  39. #
  40. # moveX = 1100
  41. # moveY = 600
  42. # pyautogui.moveTo(moveX, moveY, 0.5)
  43. # pyautogui.click(moveX, moveY)
  44. try:
  45. fileFullPath = FilePath
  46. print("fileFullPath start")
  47. # print(fileFullPath)
  48. fileFullPath = fileFullPath.replace("\\\\", "\\")
  49. # fileFullPath.replace("\\", "\\")
  50. # fileFullPath.replace("ppp", "")
  51. # print("fileFullPath")
  52. print(fileFullPath)
  53. # app = xw.App(visible=True, add_book=False)
  54. # app.display_alerts = False
  55. # app.screen_updating = True
  56. # print("init app")
  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. print(datetime.datetime.now())
  65. try:
  66. # 打开文件
  67. # pythoncom.CoInitialize()
  68. # pyautogui.hotkey('win','r')
  69. # pyautogui.sleep(0.5)
  70. # time.sleep(2)
  71. # pyperclip.copy(fileFullPath)
  72. # pyautogui.hotkey('ctrl', 'v')
  73. # pyautogui.keyDown('enter')
  74. # pyautogui.keyUp('enter')
  75. # time.sleep(5)
  76. #
  77. # # open full screen
  78. # hwnd = win32gui.GetForegroundWindow()
  79. # win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
  80. # time.sleep(1)
  81. # 启动程序
  82. code = exePath + " " + fileFullPath
  83. # print(code)
  84. autoit.run(code)
  85. autoit.win_wait_active(file_name_ext, timeout=20) # 等待窗口激活
  86. autoit.win_move(file_name_ext, 0, 0, 1024, 768)
  87. # autoit.win_move(file_name_ext, 0, 0, 1920, 1080)
  88. print("监听成功了")
  89. except Exception as e:
  90. print("打开", fileFullPath, "失败:", e)
  91. hwnd = win32gui.GetForegroundWindow()
  92. found_title = win32gui.GetWindowText(hwnd)
  93. print("窗口名称:", found_title)
  94. if found_title == "":
  95. print("窗口未打开")
  96. return
  97. if found_title.startswith(file_name_ext) == False:
  98. print("窗口与excel不一致,当前窗口:", found_title, ";excel:", file_name_ext)
  99. return
  100. autoit.win_move_by_handle(hwnd, 0, 0, 1024, 768)
  101. # autoit.win_move_by_handle(hwnd, 0, 0, 1920, 1080)
  102. ### 随机2-3次点击 单元格区域(每次点击停留1s)
  103. r = random.randint(2, 3)
  104. for i in range(r):
  105. pointX = random.randint(pointMinX, pointMaxX)
  106. pointY = random.randint(pointMinY, pointMaxY)
  107. print("随机点击", i + 1, "下;", "点击X:", pointX, ";Y:", pointY)
  108. pyautogui.moveTo(pointX, pointY, 0.5)
  109. pyautogui.click()
  110. time.sleep(1)
  111. print("start dataBtn")
  112. pyautogui.moveTo(gl_button_x, gl_button_y, 0.5)
  113. pyautogui.click()
  114. time.sleep(2)
  115. print("end dataBtn")
  116. pointX = random.randint(pointMinX, pointMaxX)
  117. pointY = random.randint(pointMinY, pointMaxY)
  118. pyautogui.moveTo(pointX, pointY)
  119. pyautogui.click()
  120. print("点击X:", pointX, ";Y:", pointY)
  121. # 停留随机秒数
  122. sleepSec = random.randint(1, 3)
  123. time.sleep(sleepSec)
  124. pointX = random.randint(pointMinX, pointMaxX)
  125. pointY = random.randint(pointMinY, pointMaxY)
  126. pyautogui.moveTo(pointX, pointY)
  127. pyautogui.click()
  128. print("点击X:", pointX, ";Y:", pointY)
  129. # 停留随机秒数
  130. sleepSec = random.randint(1, 3)
  131. time.sleep(sleepSec)
  132. print("start getCurrentPageData")
  133. pyautogui.moveTo(gl_refresh_button_x, gl_refresh_button_y, 1)
  134. pyautogui.click()
  135. time.sleep(1)
  136. print("end getCurrentPageData")
  137. pointX = random.randint(pointMinX, pointMaxX)
  138. pointY = random.randint(pointMinY, pointMaxY)
  139. pyautogui.moveTo(pointX, pointY, 0.5)
  140. pyautogui.click()
  141. print("点击X:", pointX, ";Y:", pointY)
  142. # 停留等待数据更新
  143. time.sleep(get_data_timeout_time)
  144. pyautogui.hotkey('ctrl', 's')
  145. time.sleep(1)
  146. pyautogui.hotkey('ctrl', 'w')
  147. return True
  148. # wb.save()
  149. #
  150. # wb.close()
  151. else:
  152. print("ext err:" + fileFullPath)
  153. # app.kill()
  154. return True
  155. except Exception as e:
  156. print("Exception:")
  157. print(str(e))
  158. return False
  159. if __name__ == "__main__":
  160. app = __hug__.http.server()
  161. run(app=app, reloader=True, port=7007)