File size: 1,843 Bytes
fafd649
 
 
 
 
 
 
 
eba8462
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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