feat: 增加 Temp 目录与受保护视图拦截的智能逃逸机制
This commit is contained in:
+18
-1
@@ -43,10 +43,22 @@ def export_single_doc_tables(word_app, excel_app, doc_path, excel_path, log_func
|
|||||||
|
|
||||||
doc = None
|
doc = None
|
||||||
wb = None
|
wb = None
|
||||||
|
file_to_clean = None
|
||||||
try:
|
try:
|
||||||
if debug and log_func:
|
if debug and log_func:
|
||||||
log_func(f" [Debug] 正在调用 COM 接口打开文档: {abs_doc}")
|
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
|
table_count = doc.Tables.Count
|
||||||
|
|
||||||
if table_count == 0:
|
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)
|
doc.Close(SaveChanges=False)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
if file_to_clean and os.path.exists(file_to_clean):
|
||||||
|
try:
|
||||||
|
os.remove(file_to_clean)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user