From 6187e4dee81f77d948184292afde57110111cf3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Wed, 17 Jun 2026 21:31:37 +0400 Subject: [PATCH] =?UTF-8?q?camelot-py=202.0=20(pdfium,=20=D0=B1=D0=B5?= =?UTF-8?q?=D0=B7=20Ghostscript/OpenCV),=20v1.31?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 2 +- site/parser.py | 33 +++++++++++++++++++-------------- site/templates/upload.html | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/requirements.txt b/requirements.txt index d3a0b37..7a285f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,6 @@ requests psycopg2-binary python-dotenv pdfplumber -tabula-py +camelot-py>=2.0 httpx h2 diff --git a/site/parser.py b/site/parser.py index 08bc465..be3ad49 100644 --- a/site/parser.py +++ b/site/parser.py @@ -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: diff --git a/site/templates/upload.html b/site/templates/upload.html index a2dc4ec..58e93e5 100644 --- a/site/templates/upload.html +++ b/site/templates/upload.html @@ -59,7 +59,7 @@
Nubes - Сверка договоров v1.30 + Сверка договоров v1.31