jithenderchoudary commited on
Commit
b1b241b
·
verified ·
1 Parent(s): ab67ca5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pdfplumber
3
+ import pandas as pd
4
+ from io import BytesIO
5
+
6
+ def is_header(text):
7
+ """Identify headers using common keywords."""
8
+ keywords = ['Purchase Order', 'Supplier Order', 'GSTIN', 'Annexure', 'Terms', 'Currency']
9
+ return any(keyword in text for keyword in keywords)
10
+
11
+ def extract_cleaned_tables(pdf_file):
12
+ """Extract tables while skipping headers and arranging them by pages."""
13
+ tables = []
14
+
15
+ with pdfplumber.open(pdf_file) as pdf:
16
+ for page_num, page in enumerate(pdf.pages):
17
+ text = page.extract_text()
18
+
19
+ # Skip pages with header-heavy content
20
+ if is_header(text):
21
+ continue
22
+
23
+ page_tables = page.extract_tables()
24
+ for table in page_tables:
25
+ if table:
26
+ df = pd.DataFrame(table[1:], columns=table[0])
27
+ # Fix misalignment issues (if 'Unit' in wrong columns, move it)
28
+ if 'Delivery Date' in df.columns and 'Unit' in df.columns:
29
+ mask = df['Delivery Date'].str.contains(r'NOS|PCS', na=False)
30
+ df.loc[mask, 'Unit'] = df.loc[mask, 'Delivery Date']
31
+ df.loc[mask, 'Delivery Date'] = None
32
+
33
+ tables.append((f"Page_{page_num+1}", df))
34
+
35
+ return tables
36
+
37
+ # Streamlit App
38
+ st.title("Enhanced PO Extraction Tool")
39
+
40
+ uploaded_file = st.file_uploader("Upload PO PDF", type=["pdf"])
41
+
42
+ if uploaded_file:
43
+ try:
44
+ # Extract and clean tables from the uploaded PDF
45
+ extracted_tables = extract_cleaned_tables(uploaded_file)
46
+
47
+ if extracted_tables:
48
+ st.success("Tables extracted successfully!")
49
+
50
+ # Create an Excel file with multiple sheets
51
+ excel_buffer = BytesIO()
52
+ with pd.ExcelWriter(excel_buffer, engine='openpyxl') as writer:
53
+ for sheet_name, df in extracted_tables:
54
+ df.to_excel(writer, index=False, sheet_name=sheet_name)
55
+ excel_buffer.seek(0)
56
+
57
+ # Provide download options
58
+ st.download_button(
59
+ label="Download as Excel",
60
+ data=excel_buffer,
61
+ file_name="po_data.xlsx",
62
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
63
+ )
64
+ else:
65
+ st.warning("No valid tables found.")
66
+ except Exception as e:
67
+ st.error(f"An error occurred: {e}")