diff --git a/autocompra7.py b/autocompra7.py index 8fc17da..3e3a96a 100644 --- a/autocompra7.py +++ b/autocompra7.py @@ -22,9 +22,22 @@ os.makedirs(output_dir, exist_ok=True) exclude_keywords = [ "TARJETA BANCARIA", "IVA BASE IMPONIBLE", "CUOTA", "TOTAL", "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): reader = PdfReader(file_path) text = "" @@ -56,9 +69,10 @@ def extract_data_from_pdf(file_path): if name_m: cantidad = int(name_m.group(1)) producto = name_m.group(2).strip().upper() - precio_total = float(weight_m.group(1).replace(",", ".")) - products.append((fecha, cantidad, producto, - round(precio_total / cantidad, 2), precio_total)) + if not _es_producto_invalido(producto): + precio_total = float(weight_m.group(1).replace(",", ".")) + products.append((fecha, cantidad, producto, + round(precio_total / cantidad, 2), precio_total)) i += 2 continue @@ -70,7 +84,8 @@ def extract_data_from_pdf(file_path): producto = m.group(2).strip().upper() precio_unitario = float(m.group(3).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 continue except ValueError: @@ -83,8 +98,9 @@ def extract_data_from_pdf(file_path): cantidad = int(m.group(1)) producto = m.group(2).strip().upper() precio_total = float(m.group(3).replace(",", ".")) - products.append((fecha, cantidad, producto, - round(precio_total / cantidad, 2), precio_total)) + if not _es_producto_invalido(producto): + products.append((fecha, cantidad, producto, + round(precio_total / cantidad, 2), precio_total)) i += 1 continue except ValueError: