Enhance OCR logic and GUI logging

This commit is contained in:
HONOR
2026-09-20 09:54:39 +08:00
parent c07f958d40
commit 8dcd7c4031
4 changed files with 188 additions and 66 deletions
+50 -29
View File
@@ -2,36 +2,52 @@ import os
import pythoncom
import win32com.client as win32
def detect_and_create_apps():
def detect_and_create_apps(engine_choice="自动判断 (推荐)"):
"""探测并绑定 Microsoft Office 或 WPS 引擎"""
# 1. 探测 MS Office
try:
word = win32.gencache.EnsureDispatch('Word.Application')
excel = win32.gencache.EnsureDispatch('Excel.Application')
return word, excel, "Microsoft Office"
except Exception:
def try_ms_office():
try:
word = win32.Dispatch('Word.Application')
excel = win32.Dispatch('Excel.Application')
word = win32.gencache.EnsureDispatch('Word.Application')
excel = win32.gencache.EnsureDispatch('Excel.Application')
return word, excel, "Microsoft Office"
except Exception:
pass
# 2. 探测 WPS Office
try:
try:
word = win32.Dispatch('Word.Application')
excel = win32.Dispatch('Excel.Application')
return word, excel, "Microsoft Office"
except Exception:
return None
def try_wps():
try:
word = win32.gencache.EnsureDispatch('wps.Application')
try:
word = win32.gencache.EnsureDispatch('wps.Application')
except Exception:
word = win32.Dispatch('kwps.Application')
try:
excel = win32.gencache.EnsureDispatch('et.Application')
except Exception:
excel = win32.Dispatch('ket.Application')
return word, excel, "WPS Office"
except Exception:
word = win32.Dispatch('kwps.Application')
return None
try:
excel = win32.gencache.EnsureDispatch('et.Application')
except Exception:
excel = win32.Dispatch('ket.Application')
return word, excel, "WPS Office"
except Exception:
pass
if engine_choice == "Microsoft Office":
res = try_ms_office()
if res: return res
raise RuntimeError("未检测到可用的 Microsoft Office!请尝试选择 WPS Office 或 自动判断。")
elif engine_choice == "WPS Office":
res = try_wps()
if res: return res
raise RuntimeError("未检测到可用的 WPS Office!请尝试选择 Microsoft Office 或 自动判断。")
else:
# 自动判断:先看当前机器上哪个注册表优先,通常按原逻辑
res = try_ms_office()
if res: return res
res = try_wps()
if res: return res
raise RuntimeError("未检测到可用的办公套件!请确保电脑已安装 Microsoft Office 或 WPS。")
@@ -49,20 +65,25 @@ def export_single_doc_tables(word_app, excel_app, doc_path, excel_path, log_func
log_func(f" [Debug] 正在调用 COM 接口打开文档: {abs_doc}")
try:
doc = word_app.Documents.Open(FileName=abs_doc, ReadOnly=True, Visible=debug, ConfirmConversions=False)
doc = word_app.Documents.Open(abs_doc)
# 尝试访问属性,如果在“受保护的视图”下,这行会抛出 0x80020009 异常
table_count = doc.Tables.Count
except Exception as e:
if debug and log_func:
log_func(f" [Debug] 打开失败,疑似遇到受保护的视图拦截,尝试启动智能复制逃逸机制...")
import shutil
log_func(f" [Debug] 访问受阻(可能是受保护的视图),启动智能复制逃逸机制...")
out_dir = os.path.dirname(abs_excel)
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, ConfirmConversions=False)
table_count = doc.Tables.Count
# 使用原生读写剥离 Windows 的 Zone.Identifier (MOTW) 流
with open(abs_doc, 'rb') as f_in:
with open(file_to_clean, 'wb') as f_out:
f_out.write(f_in.read())
doc = word_app.Documents.Open(file_to_clean)
table_count = doc.Tables.Count
if table_count == 0:
if log_func: