camelot для таблиц PDF (lattice+stream), fallback на pdfplumber, v1.27
This commit is contained in:
+39
-1
@@ -123,7 +123,15 @@ def _parse_doc(file_bytes: bytes) -> dict:
|
|||||||
|
|
||||||
def _parse_pdf(file_bytes: bytes) -> dict:
|
def _parse_pdf(file_bytes: bytes) -> dict:
|
||||||
result = {"elements": [], "errors": []}
|
result = {"elements": [], "errors": []}
|
||||||
|
import tempfile, os as _os
|
||||||
|
|
||||||
|
# Сохранить PDF во временный файл (camelot требует путь)
|
||||||
|
tmp_path = None
|
||||||
try:
|
try:
|
||||||
|
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp:
|
||||||
|
tmp.write(file_bytes)
|
||||||
|
tmp_path = tmp.name
|
||||||
|
|
||||||
import pdfplumber
|
import pdfplumber
|
||||||
|
|
||||||
with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
|
with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
|
||||||
@@ -142,7 +150,34 @@ def _parse_pdf(file_bytes: bytes) -> dict:
|
|||||||
"page": pn,
|
"page": pn,
|
||||||
})
|
})
|
||||||
|
|
||||||
# Таблицы на странице
|
# Таблицы — camelot (точнее), иначе pdfplumber
|
||||||
|
tables_found = False
|
||||||
|
try:
|
||||||
|
import camelot
|
||||||
|
# Сначала lattice (таблицы с рамками)
|
||||||
|
for flavor in ("lattice", "stream"):
|
||||||
|
try:
|
||||||
|
ctables = camelot.read_pdf(tmp_path, pages="all", flavor=flavor)
|
||||||
|
for ct in ctables:
|
||||||
|
rows = [list(ct.df.columns)] + ct.df.values.tolist()
|
||||||
|
result["elements"].append({
|
||||||
|
"type": "table",
|
||||||
|
"rows": rows,
|
||||||
|
"page": ct.page,
|
||||||
|
})
|
||||||
|
tables_found = True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
except ImportError:
|
||||||
|
result["errors"].append("camelot not installed, using pdfplumber")
|
||||||
|
except Exception as e:
|
||||||
|
result["errors"].append(f"camelot error: {e}")
|
||||||
|
|
||||||
|
# Fallback: pdfplumber таблицы если camelot ничего не нашёл
|
||||||
|
if not tables_found:
|
||||||
|
with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
|
||||||
|
for page in pdf.pages:
|
||||||
|
pn = page.page_number
|
||||||
tables = page.extract_tables()
|
tables = page.extract_tables()
|
||||||
for tbl in tables:
|
for tbl in tables:
|
||||||
if tbl:
|
if tbl:
|
||||||
@@ -154,6 +189,9 @@ def _parse_pdf(file_bytes: bytes) -> dict:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
result["errors"].append(f"pdf parse error: {e}")
|
result["errors"].append(f"pdf parse error: {e}")
|
||||||
|
finally:
|
||||||
|
if tmp_path and _os.path.exists(tmp_path):
|
||||||
|
_os.unlink(tmp_path)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="topbar">
|
<div class="topbar">
|
||||||
<img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes">
|
<img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes">
|
||||||
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.26</span></span>
|
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.27</span></span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="content">
|
<div class="content">
|
||||||
|
|||||||
Reference in New Issue
Block a user