refactor: 将克隆文件的逃逸目录明确指定为导出目录下的 temp 子文件夹并加入清空销毁逻辑
This commit is contained in:
@@ -55,7 +55,10 @@ def export_single_doc_tables(word_app, excel_app, doc_path, excel_path, 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)}")
|
||||
temp_dir = os.path.join(out_dir, "temp")
|
||||
if not os.path.exists(temp_dir):
|
||||
os.makedirs(temp_dir, exist_ok=True)
|
||||
file_to_clean = os.path.join(temp_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)
|
||||
|
||||
@@ -126,5 +129,8 @@ def export_single_doc_tables(word_app, excel_app, doc_path, excel_path, log_func
|
||||
if file_to_clean and os.path.exists(file_to_clean):
|
||||
try:
|
||||
os.remove(file_to_clean)
|
||||
temp_dir = os.path.dirname(file_to_clean)
|
||||
if not os.listdir(temp_dir):
|
||||
os.rmdir(temp_dir)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user