import pdfplumber import pandas as pd import gradio as gr # Define function to extract data def extract_data(pdf_file): data = [] columns = ["SI No", "Material Description", "Material Number", "HSN Code", "IGST", "Unit", "Quantity", "Dely Qty", "Dely Date", "Unit Rate", "Value", "Purchase Order No", "Date"] # Example Purchase Order Details (Adjust accordingly if needed) purchase_order_no = "PO12345" purchase_order_date = "04.11.2024" with pdfplumber.open(pdf_file) as pdf: for page in pdf.pages: text = page.extract_text().splitlines() for line in text: parts = line.split() try: si_no = int(parts[0]) if si_no % 10 == 0: # Assuming SI numbers are in multiples of 10 as per the sample material_desc = " ".join(parts[1:2]) material_number = parts[3] if "Material" in parts else "220736540000" # Use a default number if missing hsn_code = "8310" # Fixed as per sample; adjust if required igst = "18%" # Fixed IGST as per sample; adjust if required unit = parts[4] quantity = int(parts[5]) dely_qty = int(parts[6]) dely_date = parts[7] unit_rate = float(parts[8]) value = float(parts[9]) # Append extracted data to maintain the order as per the sample screenshot data.append([ si_no, material_desc, material_number, hsn_code, igst, unit, quantity, dely_qty, dely_date, unit_rate, value, purchase_order_no, purchase_order_date ]) except (ValueError, IndexError): continue df = pd.DataFrame(data, columns=columns) excel_path = "/tmp/Extracted_Purchase_Order_Data.xlsx" df.to_excel(excel_path, index=False) return excel_path # Set up Gradio interface iface = gr.Interface( fn=extract_data, inputs=gr.File(label="Upload PDF"), outputs=gr.File(label="Download Excel"), title="PDF Data Extractor" ) # Launch the app iface.launch()