feat: F3 — extracto de PDF en notificaciones y comando /pdf (pypdf, sin disco)
This commit is contained in:
43
pdf_extractor.py
Normal file
43
pdf_extractor.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""
|
||||
pdf_extractor.py — Extracción de texto de PDFs judiciales.
|
||||
|
||||
Sin LLM. Sin interpretación. Solo texto crudo.
|
||||
Los PDFs nunca se guardan en disco — se procesan en memoria.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import re
|
||||
|
||||
from pypdf import PdfReader
|
||||
|
||||
MAX_CHARS = 600
|
||||
|
||||
|
||||
def extraer_extracto(pdf_bytes: bytes) -> str | None:
|
||||
"""
|
||||
Extrae las primeras líneas de texto de un PDF judicial.
|
||||
|
||||
Retorna el extracto como string, o None si el PDF no tiene texto
|
||||
extraíble (PDF escaneado, imagen, error de parseo).
|
||||
"""
|
||||
try:
|
||||
reader = PdfReader(io.BytesIO(pdf_bytes))
|
||||
texto = ""
|
||||
for page in reader.pages:
|
||||
texto += page.extract_text() or ""
|
||||
if len(texto) >= MAX_CHARS:
|
||||
break
|
||||
|
||||
texto = texto.strip()
|
||||
if not texto:
|
||||
return None
|
||||
|
||||
texto = re.sub(r"\n{3,}", "\n\n", texto)
|
||||
|
||||
if len(texto) > MAX_CHARS:
|
||||
texto = texto[:MAX_CHARS] + "..."
|
||||
|
||||
return texto
|
||||
except Exception:
|
||||
return None
|
||||
Reference in New Issue
Block a user