feat: 添加提取耗时悬浮计时器,并修复无表格时引发的 IndexError
This commit is contained in:
@@ -7,6 +7,7 @@ from tkinter import filedialog, messagebox, ttk
|
|||||||
import pythoncom
|
import pythoncom
|
||||||
import windnd
|
import windnd
|
||||||
import datetime
|
import datetime
|
||||||
|
import time
|
||||||
|
|
||||||
from utils.config import CONFIG_FILE, APP_DIR
|
from utils.config import CONFIG_FILE, APP_DIR
|
||||||
from core.extractor import detect_and_create_apps, export_single_doc_tables
|
from core.extractor import detect_and_create_apps, export_single_doc_tables
|
||||||
@@ -174,6 +175,10 @@ class BatchTableExtractorApp:
|
|||||||
sb.pack(side=tk.RIGHT, fill=tk.Y)
|
sb.pack(side=tk.RIGHT, fill=tk.Y)
|
||||||
self.txt.config(yscrollcommand=sb.set)
|
self.txt.config(yscrollcommand=sb.set)
|
||||||
|
|
||||||
|
# 添加耗时悬浮标签
|
||||||
|
self.lbl_timer = tk.Label(self.txt, text="00:00:00.000", font=("Consolas", 10), bg="white", fg="gray")
|
||||||
|
self.lbl_timer.place(relx=1.0, rely=0.0, anchor=tk.NE, x=-10, y=5)
|
||||||
|
|
||||||
def _on_debug_toggle(self):
|
def _on_debug_toggle(self):
|
||||||
if self.debug_var.get():
|
if self.debug_var.get():
|
||||||
messagebox.showinfo("Debug 模式说明", "开启 Debug 模式后,程序在提取表格时将强制显示 Office/WPS 窗口。\n\n这主要用于排查程序“卡死”的问题。例如:当文档被加密或需要修复时,Office 会在后台弹窗等待用户点击。开启此模式可让你看到这些弹窗并手动处理。")
|
messagebox.showinfo("Debug 模式说明", "开启 Debug 模式后,程序在提取表格时将强制显示 Office/WPS 窗口。\n\n这主要用于排查程序“卡死”的问题。例如:当文档被加密或需要修复时,Office 会在后台弹窗等待用户点击。开启此模式可让你看到这些弹窗并手动处理。")
|
||||||
@@ -282,6 +287,16 @@ class BatchTableExtractorApp:
|
|||||||
self._save_config()
|
self._save_config()
|
||||||
self.root.destroy()
|
self.root.destroy()
|
||||||
|
|
||||||
|
def _update_timer(self):
|
||||||
|
if getattr(self, '_is_running', False):
|
||||||
|
elapsed = time.time() - getattr(self, '_start_time', time.time())
|
||||||
|
hours, rem = divmod(elapsed, 3600)
|
||||||
|
minutes, seconds = divmod(rem, 60)
|
||||||
|
ms = int((elapsed - int(elapsed)) * 1000)
|
||||||
|
time_str = f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}.{ms:03d}"
|
||||||
|
self.lbl_timer.config(text=time_str)
|
||||||
|
self.root.after(50, self._update_timer)
|
||||||
|
|
||||||
def _start(self):
|
def _start(self):
|
||||||
self._save_config()
|
self._save_config()
|
||||||
src = self.src_path_var.get().strip()
|
src = self.src_path_var.get().strip()
|
||||||
@@ -332,6 +347,9 @@ class BatchTableExtractorApp:
|
|||||||
|
|
||||||
self.log(f"\n===== 开始任务: 共扫描到 {len(files_to_process)} 个文件 =====")
|
self.log(f"\n===== 开始任务: 共扫描到 {len(files_to_process)} 个文件 =====")
|
||||||
|
|
||||||
|
self._start_time = time.time()
|
||||||
|
self._update_timer()
|
||||||
|
|
||||||
threading.Thread(
|
threading.Thread(
|
||||||
target=self._worker,
|
target=self._worker,
|
||||||
args=(files_to_process, out),
|
args=(files_to_process, out),
|
||||||
|
|||||||
+20
-15
@@ -73,27 +73,32 @@ def main():
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
tables_found = 0
|
tables_found = 0
|
||||||
with pd.ExcelWriter(excel_path, engine='openpyxl') as writer:
|
extracted_tables = []
|
||||||
for page_num, img_array in images:
|
|
||||||
print(f"[OCR] Scanning page {page_num}...")
|
|
||||||
try:
|
|
||||||
table_result, _ = table_engine(img_array)
|
|
||||||
html_str = table_result
|
|
||||||
if html_str:
|
|
||||||
dfs = pd.read_html(html_str)
|
|
||||||
for i, df in enumerate(dfs):
|
|
||||||
tables_found += 1
|
|
||||||
sheet_name = f"P{page_num}_T{i+1}"
|
|
||||||
df.to_excel(writer, sheet_name=sheet_name, index=False, header=False)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error processing page {page_num}: {e}")
|
|
||||||
|
|
||||||
if tables_found == 0:
|
for page_num, img_array in images:
|
||||||
|
print(f"[OCR] Scanning page {page_num}...")
|
||||||
|
try:
|
||||||
|
table_result, _ = table_engine(img_array)
|
||||||
|
html_str = table_result
|
||||||
|
if html_str:
|
||||||
|
dfs = pd.read_html(html_str)
|
||||||
|
for i, df in enumerate(dfs):
|
||||||
|
tables_found += 1
|
||||||
|
sheet_name = f"P{page_num}_T{i+1}"
|
||||||
|
extracted_tables.append((sheet_name, df))
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error processing page {page_num}: {e}")
|
||||||
|
|
||||||
|
if not extracted_tables:
|
||||||
print("No tables detected.")
|
print("No tables detected.")
|
||||||
if os.path.exists(excel_path):
|
if os.path.exists(excel_path):
|
||||||
os.remove(excel_path)
|
os.remove(excel_path)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
with pd.ExcelWriter(excel_path, engine='openpyxl') as writer:
|
||||||
|
for sheet_name, df in extracted_tables:
|
||||||
|
df.to_excel(writer, sheet_name=sheet_name, index=False, header=False)
|
||||||
|
|
||||||
print(f"Successfully extracted {tables_found} tables.")
|
print(f"Successfully extracted {tables_found} tables.")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user