diff --git a/src/core/extractor.py b/src/core/extractor.py index e7e5974..f6130fd 100644 --- a/src/core/extractor.py +++ b/src/core/extractor.py @@ -43,10 +43,22 @@ def export_single_doc_tables(word_app, excel_app, doc_path, excel_path, log_func doc = None wb = None + file_to_clean = None try: if debug and log_func: log_func(f" [Debug] 正在调用 COM 接口打开文档: {abs_doc}") - doc = word_app.Documents.Open(FileName=abs_doc, ReadOnly=True, Visible=debug) + + try: + doc = word_app.Documents.Open(FileName=abs_doc, ReadOnly=True, Visible=debug) + except Exception as e: + if debug and log_func: + log_func(f" [Debug] 打开失败,疑似遇到受保护的视图拦截,尝试启动智能复制逃逸机制...") + import shutil + out_dir = os.path.dirname(abs_excel) + file_to_clean = os.path.join(out_dir, f"~temp_bypass_{os.path.basename(abs_doc)}") + shutil.copy2(abs_doc, file_to_clean) + doc = word_app.Documents.Open(FileName=file_to_clean, ReadOnly=True, Visible=debug) + table_count = doc.Tables.Count if table_count == 0: @@ -111,3 +123,8 @@ def export_single_doc_tables(word_app, excel_app, doc_path, excel_path, log_func doc.Close(SaveChanges=False) except Exception: pass + if file_to_clean and os.path.exists(file_to_clean): + try: + os.remove(file_to_clean) + except Exception: + pass