Spaces:
Running
Running
| import pandas as pd | |
| import pdfplumber | |
| import re | |
| # ========================================== | |
| # NEW: GENERIC LINED TABLE PARSER | |
| # ========================================== | |
| def parse_generic_pdf(pdf_path: str) -> list: | |
| """Extracts standard tables with explicit drawn borders.""" | |
| data_tree = [] | |
| with pdfplumber.open(pdf_path) as pdf: | |
| for page_num, page in enumerate(pdf.pages): | |
| # Native extraction using drawn lines | |
| tables = page.extract_tables() | |
| for table_idx, table in enumerate(tables): | |
| if not table or len(table) < 2: | |
| continue | |
| # Clean up None types and newlines | |
| cleaned_table = [ | |
| [str(cell).replace('\n', ' ').strip() if cell else "" for cell in row] | |
| for row in table | |
| ] | |
| headers = cleaned_table[0] | |
| items = [] | |
| # Build the JSON structure to match our standard format | |
| for row in cleaned_table[1:]: | |
| row_data = {} | |
| for col_idx, cell in enumerate(row): | |
| header_name = headers[col_idx] if col_idx < len(headers) and headers[ | |
| col_idx] else f"Column_{col_idx + 1}" | |
| row_data[header_name] = cell | |
| items.append({ | |
| "particulars": row[0] if row else "", | |
| "data": row_data | |
| }) | |
| data_tree.append({ | |
| "name": f"Page {page_num + 1} - Lined Table {table_idx + 1}", | |
| "type": "generic_table", | |
| "items": items, | |
| "raw_table": cleaned_table # We pass this for easy Pandas rendering | |
| }) | |
| return data_tree |