rpa_kpler_refresh.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import hug
  2. from bottle import route, run
  3. import pyautogui
  4. import random
  5. import time
  6. import os
  7. import win32gui
  8. import autoit
  9. import datetime
  10. hug.API(__name__).http.output_format = hug.output_format.json
  11. # exePath = r"C:\Program Files (x86)\Kingsoft\WPS Office\11.8.2.12085\office6\et"
  12. # exePath = exePath = r"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"
  13. # exePath = exePath = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
  14. exePath = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
  15. # 顶部Kpler按钮的x,y位置
  16. kp_button_x = 920
  17. kp_button_y = 100
  18. # Kpler选项卡下面的table选择按钮的x,y位置
  19. kp_table_select_button_x = 760
  20. kp_table_select_button_y = 345
  21. # Kpler选项卡下面的flow选择按钮的x,y位置
  22. kp_flow_select_button_x = 760
  23. kp_flow_select_button_y = 435
  24. # Kpler选项卡下面的刷新按钮的x,y位置
  25. kp_refresh_button_x = 850
  26. kp_refresh_button_y = 300
  27. # 获取数据的超时时间(单位:s)
  28. get_data_timeout_time = 30
  29. # 随机点击的按钮范围
  30. pointMinX = 200
  31. pointMaxX = 650
  32. pointMinY = 300
  33. pointMaxY = 550
  34. @hug.get('/kpler/server')
  35. def kpler_server():
  36. return 1
  37. @hug.get('/kpler/refresh')
  38. def kpler_refresh(FilePath):
  39. screen_width, screen_height = pyautogui.size()
  40. print("屏幕分辨率:{} x {}".format(screen_width, screen_height))
  41. pyautogui.FAILSAFE = False
  42. FilePath = FilePath.replace('"', '')
  43. pyautogui.hotkey('win', 'd')
  44. try:
  45. fileFullPath = FilePath
  46. print("fileFullPath start")
  47. fileFullPath = fileFullPath.replace("\\\\", "\\")
  48. print(fileFullPath)
  49. if os.path.exists(fileFullPath) and ".xlsx" in fileFullPath \
  50. and fileFullPath.find("~$") < 0 \
  51. and fileFullPath.find("template") < 0:
  52. # 获取文件名和扩展名
  53. file_name, file_ext = os.path.splitext(os.path.basename(fileFullPath))
  54. file_name_ext = file_name + file_ext
  55. print(file_name_ext)
  56. print(datetime.datetime.now())
  57. try:
  58. # 启动程序
  59. code = exePath + " " + fileFullPath
  60. autoit.run(code)
  61. autoit.win_wait_active(file_name_ext, timeout=20) # 等待窗口激活
  62. autoit.win_move(file_name_ext, 0, 0, 1024, 768)
  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. ### 随机2-3次点击 单元格区域(每次点击停留1s)
  81. r = random.randint(2, 3)
  82. for i in range(r):
  83. pointX = random.randint(pointMinX, pointMaxX)
  84. pointY = random.randint(pointMinY, pointMaxY)
  85. print("随机点击", i + 1, "下;", "点击X:", pointX, ";Y:", pointY)
  86. pyautogui.moveTo(pointX, pointY, 0.5)
  87. pyautogui.click()
  88. time.sleep(1)
  89. print("start dataBtn")
  90. pyautogui.moveTo(kp_button_x, kp_button_y, 0.5)
  91. pyautogui.click()
  92. time.sleep(5)
  93. print("end dataBtn")
  94. pointX = random.randint(pointMinX, pointMaxX)
  95. pointY = random.randint(pointMinY, pointMaxY)
  96. pyautogui.moveTo(pointX, pointY)
  97. pyautogui.click()
  98. print("点击X:", pointX, ";Y:", pointY)
  99. # 停留随机秒数
  100. sleepSec = random.randint(1, 3)
  101. time.sleep(sleepSec)
  102. pointX = random.randint(pointMinX, pointMaxX)
  103. pointY = random.randint(pointMinY, pointMaxY)
  104. pyautogui.moveTo(pointX, pointY)
  105. pyautogui.click()
  106. print("点击X:", pointX, ";Y:", pointY)
  107. # 停留随机秒数
  108. sleepSec = random.randint(1, 3)
  109. time.sleep(sleepSec)
  110. print("start tableSelect")
  111. pyautogui.moveTo(kp_table_select_button_x, kp_table_select_button_y, 1)
  112. pyautogui.click()
  113. time.sleep(1)
  114. print("end tableSelect")
  115. print("start flowSelect")
  116. pyautogui.moveTo(kp_flow_select_button_x, kp_flow_select_button_y, 1)
  117. pyautogui.click()
  118. time.sleep(1)
  119. print("end flowSelect")
  120. print("start getCurrentPageData")
  121. pyautogui.moveTo(kp_refresh_button_x, kp_refresh_button_y, 1)
  122. pyautogui.click()
  123. time.sleep(1)
  124. print("end getCurrentPageData")
  125. pointX = random.randint(pointMinX, pointMaxX)
  126. pointY = random.randint(pointMinY, pointMaxY)
  127. pyautogui.moveTo(pointX, pointY, 0.5)
  128. pyautogui.click()
  129. print("点击X:", pointX, ";Y:", pointY)
  130. # 停留等待数据更新
  131. time.sleep(get_data_timeout_time)
  132. pyautogui.hotkey('ctrl', 's')
  133. time.sleep(1)
  134. pyautogui.hotkey('ctrl', 'w')
  135. return True
  136. else:
  137. print("ext err:" + fileFullPath)
  138. return True
  139. except Exception as e:
  140. print("Exception:")
  141. print(str(e))
  142. return False
  143. if __name__ == "__main__":
  144. app = __hug__.http.server()
  145. run(app=app, reloader=True, port=8007) # 使用不同的端口号,避免和mysteel冲突