feat: 添加提取耗时悬浮计时器,并修复无表格时引发的 IndexError
This commit is contained in:
+21
-16
@@ -73,27 +73,32 @@ def main():
|
||||
sys.exit(0)
|
||||
|
||||
tables_found = 0
|
||||
with pd.ExcelWriter(excel_path, engine='openpyxl') as writer:
|
||||
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:
|
||||
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}"
|
||||
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.")
|
||||
if os.path.exists(excel_path):
|
||||
os.remove(excel_path)
|
||||
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.")
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user