_test_ppstructure.py
2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""临时脚本:使用项目目录作为模型目录,测试 PPStructureV3"""
import os
import sys
# 指向项目内有写权限的目录(已经在 .bat 或 shell 中设置了 PADDLE_PDX_MODEL_DIR)
model_dir = os.environ.get("PADDLE_PDX_MODEL_DIR", r"d:\work1\data-analyze-webui\.paddlex_models")
os.makedirs(model_dir, exist_ok=True)
from paddleocr import PPStructureV3
print(f"Model dir: {model_dir}", flush=True)
print("Creating PPStructureV3 (use_doc_unwarping=False)...", flush=True)
engine = PPStructureV3(use_doc_unwarping=False)
print("Created! Running predict...", flush=True)
result = engine.predict(r"C:\Users\jiata\Desktop\微信图片_20260722093956_9_32.png")
print(f"\nResult type: {type(result)}, length: {len(result)}", flush=True)
for i, item in enumerate(result[:10]):
print(f"\n--- Item {i} ({type(item).__name__}) ---", flush=True)
if isinstance(item, dict):
print(f" keys: {list(item.keys())}", flush=True)
for k, v in item.items():
if k == "res" and isinstance(v, dict):
print(f" [{k}] keys={list(v.keys())}", flush=True)
if "html" in v:
print(f" html [{len(v['html'])}]: {v['html'][:500]}", flush=True)
else:
s = str(v)
if len(s) > 150:
s = s[:150] + "..."
print(f" [{k}]: {s}", flush=True)
# 保存为 Excel
htmls = []
for item in result:
if isinstance(item, dict) and item.get("type") == "table":
html = item.get("res", {}).get("html", "")
if html:
htmls.append(html)
print(f"\nFound {len(htmls)} tables", flush=True)
if htmls:
import pandas as pd
excel_path = r"d:\work1\data-analyze-webui\output_paddle_test.xlsx"
with pd.ExcelWriter(excel_path, engine="openpyxl") as writer:
for idx, html in enumerate(htmls):
dfs = pd.read_html(html)
if dfs:
dfs[0].to_excel(writer, index=False, sheet_name=f"Table_{idx+1}")
print(f"Saved to {excel_path}", flush=True)