camelot-py 2.0 (pdfium, без Ghostscript/OpenCV), v1.31
This commit is contained in:
+19
-14
@@ -124,22 +124,27 @@ def _parse_doc(file_bytes: bytes) -> dict:
|
||||
def _parse_pdf(file_bytes: bytes) -> dict:
|
||||
result = {"elements": [], "errors": []}
|
||||
|
||||
# Попробовать tabula (Java) — точнее таблицы
|
||||
tables_from_tabula = False
|
||||
# Попробовать camelot (pdfium, без системных зависимостей)
|
||||
tables_from_camelot = False
|
||||
try:
|
||||
import tabula
|
||||
tables = tabula.read_pdf(io.BytesIO(file_bytes), pages="all", multiple_tables=True)
|
||||
for tbl in tables:
|
||||
rows = [list(tbl.columns)] + tbl.values.tolist()
|
||||
result["elements"].append({
|
||||
"type": "table",
|
||||
"rows": rows,
|
||||
})
|
||||
tables_from_tabula = len(tables) > 0
|
||||
import camelot
|
||||
for flavor in ("lattice", "stream"):
|
||||
try:
|
||||
ctables = camelot.read_pdf(io.BytesIO(file_bytes), 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_from_camelot = True
|
||||
except Exception:
|
||||
pass
|
||||
except ImportError:
|
||||
result["errors"].append("tabula not installed, using pdfplumber")
|
||||
result["errors"].append("camelot not installed, using pdfplumber")
|
||||
except Exception as e:
|
||||
result["errors"].append(f"tabula error: {e}")
|
||||
result["errors"].append(f"camelot error: {e}")
|
||||
|
||||
# pdfplumber: текст + таблицы (если tabula не сработал)
|
||||
try:
|
||||
@@ -158,7 +163,7 @@ def _parse_pdf(file_bytes: bytes) -> dict:
|
||||
"text": line,
|
||||
"page": pn,
|
||||
})
|
||||
if not tables_from_tabula:
|
||||
if not tables_from_camelot:
|
||||
tables = page.extract_tables()
|
||||
for tbl in tables:
|
||||
if tbl:
|
||||
|
||||
Reference in New Issue
Block a user