feat: 增加 PDF 双引擎智能分流,本地 Word 转换电子 PDF,云端提取扫描件
This commit is contained in:
@@ -49,7 +49,7 @@ def export_single_doc_tables(word_app, excel_app, doc_path, excel_path, log_func
|
|||||||
log_func(f" [Debug] 正在调用 COM 接口打开文档: {abs_doc}")
|
log_func(f" [Debug] 正在调用 COM 接口打开文档: {abs_doc}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
doc = word_app.Documents.Open(FileName=abs_doc, ReadOnly=True, Visible=debug)
|
doc = word_app.Documents.Open(FileName=abs_doc, ReadOnly=True, Visible=debug, ConfirmConversions=False)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if debug and log_func:
|
if debug and log_func:
|
||||||
log_func(f" [Debug] 打开失败,疑似遇到受保护的视图拦截,尝试启动智能复制逃逸机制...")
|
log_func(f" [Debug] 打开失败,疑似遇到受保护的视图拦截,尝试启动智能复制逃逸机制...")
|
||||||
@@ -60,7 +60,7 @@ def export_single_doc_tables(word_app, excel_app, doc_path, excel_path, log_func
|
|||||||
os.makedirs(temp_dir, exist_ok=True)
|
os.makedirs(temp_dir, exist_ok=True)
|
||||||
file_to_clean = os.path.join(temp_dir, f"~temp_bypass_{os.path.basename(abs_doc)}")
|
file_to_clean = os.path.join(temp_dir, f"~temp_bypass_{os.path.basename(abs_doc)}")
|
||||||
shutil.copy2(abs_doc, file_to_clean)
|
shutil.copy2(abs_doc, file_to_clean)
|
||||||
doc = word_app.Documents.Open(FileName=file_to_clean, ReadOnly=True, Visible=debug)
|
doc = word_app.Documents.Open(FileName=file_to_clean, ReadOnly=True, Visible=debug, ConfirmConversions=False)
|
||||||
|
|
||||||
table_count = doc.Tables.Count
|
table_count = doc.Tables.Count
|
||||||
|
|
||||||
|
|||||||
+42
-8
@@ -45,6 +45,7 @@ class BatchTableExtractorApp:
|
|||||||
self.api_key_var = tk.StringVar()
|
self.api_key_var = tk.StringVar()
|
||||||
self.secret_key_var = tk.StringVar()
|
self.secret_key_var = tk.StringVar()
|
||||||
self.pdf_pages_var = tk.StringVar(value="")
|
self.pdf_pages_var = tk.StringVar(value="")
|
||||||
|
self.pdf_engine_var = tk.StringVar(value="智能判定 (推荐)")
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
self._stop_event = threading.Event()
|
self._stop_event = threading.Event()
|
||||||
@@ -82,6 +83,7 @@ class BatchTableExtractorApp:
|
|||||||
self.api_key_var.set(cfg.get("api_key", ""))
|
self.api_key_var.set(cfg.get("api_key", ""))
|
||||||
self.secret_key_var.set(cfg.get("secret_key", ""))
|
self.secret_key_var.set(cfg.get("secret_key", ""))
|
||||||
self.pdf_pages_var.set(cfg.get("pdf_pages", ""))
|
self.pdf_pages_var.set(cfg.get("pdf_pages", ""))
|
||||||
|
self.pdf_engine_var.set(cfg.get("pdf_engine", "智能判定 (推荐)"))
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -98,7 +100,8 @@ class BatchTableExtractorApp:
|
|||||||
"conflict_action": self.conflict_action_var.get(),
|
"conflict_action": self.conflict_action_var.get(),
|
||||||
"api_key": self.api_key_var.get(),
|
"api_key": self.api_key_var.get(),
|
||||||
"secret_key": self.secret_key_var.get(),
|
"secret_key": self.secret_key_var.get(),
|
||||||
"pdf_pages": self.pdf_pages_var.get()
|
"pdf_pages": self.pdf_pages_var.get(),
|
||||||
|
"pdf_engine": self.pdf_engine_var.get()
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
|
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
|
||||||
@@ -155,15 +158,17 @@ class BatchTableExtractorApp:
|
|||||||
self.btn_view_log = ProgressButton(path_box, text="日志", command=self._show_log_window, height=22, width=65)
|
self.btn_view_log = ProgressButton(path_box, text="日志", command=self._show_log_window, height=22, width=65)
|
||||||
self.btn_view_log.grid(row=2, column=2, sticky=tk.EW)
|
self.btn_view_log.grid(row=2, column=2, sticky=tk.EW)
|
||||||
|
|
||||||
self.lbl_pdf_pages = ttk.Label(path_box, text="PDF提取页码:")
|
ttk.Label(path_box, text="PDF 处理引擎:").grid(row=3, column=0, sticky=tk.W, pady=5)
|
||||||
self.lbl_pdf_pages.grid(row=3, column=0, sticky=tk.W, pady=5)
|
ttk.Combobox(path_box, textvariable=self.pdf_engine_var, values=["智能判定 (推荐)", "强转 Word (免费/极速)", "云端 OCR (高精度/耗额度)"], state="readonly", width=43).grid(row=3, column=1, sticky=tk.W, padx=6)
|
||||||
self.entry_pdf_pages = ttk.Entry(path_box, textvariable=self.pdf_pages_var, width=46)
|
|
||||||
self.entry_pdf_pages.grid(row=3, column=1, sticky=tk.EW, padx=6)
|
|
||||||
self.entry_pdf_pages.insert(0, "") # Placeholder text hint can be added if needed
|
|
||||||
# Create a small hint label or just rely on the label
|
|
||||||
|
|
||||||
self.btn_api_settings = ProgressButton(path_box, text="⚙️ OCR API", command=self._show_api_settings, height=22, width=65)
|
self.btn_api_settings = ProgressButton(path_box, text="⚙️ OCR API", command=self._show_api_settings, height=22, width=65)
|
||||||
self.btn_api_settings.grid(row=3, column=2, sticky=tk.EW)
|
self.btn_api_settings.grid(row=3, column=2, sticky=tk.EW)
|
||||||
|
|
||||||
|
self.lbl_pdf_pages = ttk.Label(path_box, text="PDF提取页码:")
|
||||||
|
self.lbl_pdf_pages.grid(row=4, column=0, sticky=tk.W, pady=5)
|
||||||
|
self.entry_pdf_pages = ttk.Entry(path_box, textvariable=self.pdf_pages_var, width=46)
|
||||||
|
self.entry_pdf_pages.grid(row=4, column=1, sticky=tk.EW, padx=6)
|
||||||
|
self.entry_pdf_pages.insert(0, "")
|
||||||
|
|
||||||
path_box.columnconfigure(1, weight=1)
|
path_box.columnconfigure(1, weight=1)
|
||||||
|
|
||||||
@@ -459,7 +464,36 @@ class BatchTableExtractorApp:
|
|||||||
self.root.after(0, self.log, m)
|
self.root.after(0, self.log, m)
|
||||||
|
|
||||||
ext = os.path.splitext(file_path)[1].lower()
|
ext = os.path.splitext(file_path)[1].lower()
|
||||||
if ext in ['.doc', '.docx']:
|
route_target = "word"
|
||||||
|
|
||||||
|
if ext in ['.png', '.jpg', '.jpeg']:
|
||||||
|
route_target = "ocr"
|
||||||
|
elif ext == '.pdf':
|
||||||
|
engine_pref = self.pdf_engine_var.get()
|
||||||
|
if "Word" in engine_pref:
|
||||||
|
route_target = "word"
|
||||||
|
elif "OCR" in engine_pref:
|
||||||
|
route_target = "ocr"
|
||||||
|
else:
|
||||||
|
# 智能判定:读取前3页文本量
|
||||||
|
try:
|
||||||
|
import fitz
|
||||||
|
with fitz.open(file_path) as pdoc:
|
||||||
|
text_len = 0
|
||||||
|
check_pages = min(3, len(pdoc))
|
||||||
|
for p in range(check_pages):
|
||||||
|
text_len += len(pdoc[p].get_text("text").strip())
|
||||||
|
|
||||||
|
if text_len > 100:
|
||||||
|
self.root.after(0, lambda: self.log(" [智能判定] 识别为电子版 PDF,已路由至 Word 极速引擎"))
|
||||||
|
route_target = "word"
|
||||||
|
else:
|
||||||
|
self.root.after(0, lambda: self.log(" [智能判定] 识别为扫描版 PDF,已路由至云端 OCR 引擎"))
|
||||||
|
route_target = "ocr"
|
||||||
|
except Exception:
|
||||||
|
route_target = "ocr" # fallback
|
||||||
|
|
||||||
|
if route_target == "word":
|
||||||
tc = export_single_doc_tables(word_app, excel_app, file_path, target_excel,
|
tc = export_single_doc_tables(word_app, excel_app, file_path, target_excel,
|
||||||
log_func=my_log, debug=debug_mode)
|
log_func=my_log, debug=debug_mode)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user