Ver Fonte

fix:主要解决excel崩溃后,一个空白excel的弹框占用,导致后续的excel无法更新的问题

Roc há 10 meses atrás
pai
commit
476c7b9158
1 ficheiros alterados com 19 adições e 1 exclusões
  1. 19 1
      rpa_smm_refresh.py

+ 19 - 1
rpa_smm_refresh.py

@@ -7,7 +7,7 @@ import os
 import pythoncom
 import win32gui, win32con, win32process
 import pyperclip
-# import psutil
+import psutil
 import urllib.parse
 import autoit
 import datetime
@@ -45,6 +45,9 @@ pointMaxX = 650
 pointMinY = 300
 pointMaxY = 550
 
+# 异常的监控弹框,需要关闭进程的
+err_windows_name_list = ["WPS Office","另存为"]
+
 @hug.get('/smm_chemical/refresh')
 def smm_refresh(file_path):
     screen_width, screen_height = pyautogui.size()
@@ -79,6 +82,9 @@ def smm_refresh(file_path):
             file_name_ext = file_name + file_ext
             # print(file_name_ext)
 
+            # 停止2s打开,避免打开过快导致excel崩溃
+            time.sleep(2)
+
             try:
 
                 # 启动程序
@@ -105,6 +111,18 @@ def smm_refresh(file_path):
                 if found_title == "":
                     print("窗口未打开")
                     return
+
+                # 遍历异常名称列表去校验,如果匹配上了,那么就要退出excel并打开
+                for err_windows_name in err_windows_name_list:
+                    if found_title.startswith(err_windows_name):
+                        # 获取与窗口句柄关联的进程ID
+                        process_id = win32process.GetWindowThreadProcessId(hwnd)[1]
+                        # 根据进程ID获取进程对象并尝试优雅地终止进程
+                        target_process = psutil.Process(process_id)
+                        target_process.terminate()
+                        print("可能是EXCEL崩溃导致,优雅退出EXCEL")
+                        return
+                    
                 # 如果是office的,那么就是要将file_name_ext变更为 file_name;因为office没有后缀
                 if found_title.startswith(file_name_ext) == False:
                     print("当前窗口与excel不一致,当前窗口:", found_title, ";excel:", file_name_ext)