fix: filtrar lineas de IVA (%) que se colaban como productos

This commit is contained in:
Tatiana Villa Ema 2026-04-25 19:30:40 +02:00
parent 385828a5db
commit ebb634445c
1 changed files with 23 additions and 7 deletions

View File

@ -22,9 +22,22 @@ os.makedirs(output_dir, exist_ok=True)
exclude_keywords = [ exclude_keywords = [
"TARJETA BANCARIA", "IVA BASE IMPONIBLE", "CUOTA", "TOTAL", "TARJETA BANCARIA", "IVA BASE IMPONIBLE", "CUOTA", "TOTAL",
"SE ADMITEN DEVOLUCIONES CON TICKET", "N.C", "AUT", "AID", "SE ADMITEN DEVOLUCIONES CON TICKET", "N.C", "AUT", "AID",
"Verificado por dispositivo", "Visa Credit", "IMPORTE", "TARJ. BANCARIA" "Verificado por dispositivo", "Visa Credit", "IMPORTE", "TARJ. BANCARIA",
"BASES IMPONIBLES", "BASE IMPONIBLE", "BASE CUOTA", "SUMA",
"EFECTIVO", "CAMBIO", "MASTERCARD", "MERCADONA", "TICKET",
] ]
# Nombre de producto que indica línea de IVA (p.ej. "%", "% 125,30")
def _es_producto_invalido(nombre: str) -> bool:
n = nombre.strip()
return (
not n
or n.startswith("%")
or n == "%"
or re.match(r'^%', n)
or len(n) < 2
)
def extract_data_from_pdf(file_path): def extract_data_from_pdf(file_path):
reader = PdfReader(file_path) reader = PdfReader(file_path)
text = "" text = ""
@ -56,9 +69,10 @@ def extract_data_from_pdf(file_path):
if name_m: if name_m:
cantidad = int(name_m.group(1)) cantidad = int(name_m.group(1))
producto = name_m.group(2).strip().upper() producto = name_m.group(2).strip().upper()
precio_total = float(weight_m.group(1).replace(",", ".")) if not _es_producto_invalido(producto):
products.append((fecha, cantidad, producto, precio_total = float(weight_m.group(1).replace(",", "."))
round(precio_total / cantidad, 2), precio_total)) products.append((fecha, cantidad, producto,
round(precio_total / cantidad, 2), precio_total))
i += 2 i += 2
continue continue
@ -70,7 +84,8 @@ def extract_data_from_pdf(file_path):
producto = m.group(2).strip().upper() producto = m.group(2).strip().upper()
precio_unitario = float(m.group(3).replace(",", ".")) precio_unitario = float(m.group(3).replace(",", "."))
precio_total = float(m.group(4).replace(",", ".")) precio_total = float(m.group(4).replace(",", "."))
products.append((fecha, cantidad, producto, precio_unitario, precio_total)) if not _es_producto_invalido(producto):
products.append((fecha, cantidad, producto, precio_unitario, precio_total))
i += 1 i += 1
continue continue
except ValueError: except ValueError:
@ -83,8 +98,9 @@ def extract_data_from_pdf(file_path):
cantidad = int(m.group(1)) cantidad = int(m.group(1))
producto = m.group(2).strip().upper() producto = m.group(2).strip().upper()
precio_total = float(m.group(3).replace(",", ".")) precio_total = float(m.group(3).replace(",", "."))
products.append((fecha, cantidad, producto, if not _es_producto_invalido(producto):
round(precio_total / cantidad, 2), precio_total)) products.append((fecha, cantidad, producto,
round(precio_total / cantidad, 2), precio_total))
i += 1 i += 1
continue continue
except ValueError: except ValueError: