Hemachandran07 commited on
Commit
3a67311
·
verified ·
1 Parent(s): 693d867

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -249
app.py DELETED
@@ -1,249 +0,0 @@
1
- import streamlit as st
2
- import sqlite3
3
- import pandas as pd
4
- import os
5
- import tempfile
6
- import shutil
7
- import tabula
8
- import PyPDF2
9
- import openpyxl
10
- from pathlib import Path
11
-
12
- def pdf_to_excel(pdf_path, output_path):
13
- """
14
- Convert PDF to Excel file
15
-
16
- Parameters:
17
- pdf_path (str): Path to PDF file
18
- output_path (str): Path for output Excel file
19
-
20
- Returns:
21
- str: Path to created Excel file
22
- """
23
- try:
24
- # Read PDF file
25
- tables = tabula.read_pdf(pdf_path, pages='all', multiple_tables=True)
26
-
27
- # Create Excel writer object
28
- with pd.ExcelWriter(output_path, engine='openpyxl') as writer:
29
- # If no tables were found, try to extract text and create a single table
30
- if not tables:
31
- pdf_reader = PyPDF2.PdfReader(pdf_path)
32
- text_data = []
33
-
34
- for page in pdf_reader.pages:
35
- text = page.extract_text()
36
- if text:
37
- # Split text into lines and create a simple table
38
- lines = [line.strip() for line in text.split('\n') if line.strip()]
39
- text_data.extend(lines)
40
-
41
- if text_data:
42
- df = pd.DataFrame({'Content': text_data})
43
- df.to_excel(writer, sheet_name='Sheet1', index=False)
44
- else:
45
- # Write each table to a different sheet
46
- for i, table in enumerate(tables):
47
- if not table.empty:
48
- sheet_name = f'Sheet{i+1}'
49
- table.to_excel(writer, sheet_name=sheet_name, index=False)
50
-
51
- return output_path
52
- except Exception as e:
53
- raise Exception(f"Error converting PDF to Excel: {str(e)}")
54
-
55
- def get_excel_sheets(file):
56
- """Get list of sheet names from Excel file"""
57
- return pd.ExcelFile(file).sheet_names
58
-
59
- def excel_to_database(excel_file, database_name, sheet_names=None):
60
- """
61
- Convert Excel file to SQLite database.
62
-
63
- Parameters:
64
- excel_file: File object or path
65
- database_name (str): Name for the SQLite database
66
- sheet_names (list, optional): List of sheet names to convert
67
-
68
- Returns:
69
- tuple: (database_path, conversion_info)
70
- """
71
- conversion_info = []
72
-
73
- # Create database connection
74
- conn = sqlite3.connect(database_name)
75
-
76
- try:
77
- # Read all sheets if sheet_names is None
78
- if sheet_names is None:
79
- excel_data = pd.read_excel(excel_file, sheet_name=None)
80
- else:
81
- excel_data = {sheet: pd.read_excel(excel_file, sheet_name=sheet)
82
- for sheet in sheet_names}
83
-
84
- # Convert each sheet to a table
85
- for sheet_name, df in excel_data.items():
86
- # Clean column names
87
- df.columns = [col.lower().replace(' ', '_').replace('-', '_')
88
- for col in df.columns]
89
-
90
- # Clean table name
91
- table_name = ''.join(char for char in sheet_name
92
- if char.isalnum() or char == '_').lower()
93
-
94
- # Write to database
95
- df.to_sql(table_name, conn, if_exists='replace', index=False)
96
-
97
- # Store conversion info
98
- info = {
99
- 'sheet_name': sheet_name,
100
- 'table_name': table_name,
101
- 'rows': len(df),
102
- 'columns': list(df.columns)
103
- }
104
- conversion_info.append(info)
105
-
106
- finally:
107
- conn.close()
108
-
109
- return database_name, conversion_info
110
-
111
- def get_table_preview(database_path, table_name, rows=5):
112
- """Get preview of table contents"""
113
- conn = sqlite3.connect(database_path)
114
- try:
115
- df = pd.read_sql(f"SELECT * FROM {table_name} LIMIT {rows}", conn)
116
- return df
117
- finally:
118
- conn.close()
119
-
120
- def main():
121
- st.set_page_config(page_title="File to SQLite Converter", layout="wide")
122
-
123
- st.title("PDF/Excel to SQLite Database Converter")
124
- st.write("Upload a PDF or Excel file and convert it to a SQLite database")
125
- st.info('The PDF file should contain a table', icon="ℹ")
126
-
127
- # File uploader
128
- uploaded_file = st.file_uploader(
129
- "Choose a file",
130
- type=['pdf', 'xlsx', 'xls'],
131
- help="Upload a PDF or Excel file"
132
- )
133
-
134
- if uploaded_file is not None:
135
- try:
136
- # Create temporary directory
137
- temp_dir = tempfile.mkdtemp()
138
-
139
- # Save uploaded file
140
- temp_input_path = os.path.join(temp_dir, uploaded_file.name)
141
- with open(temp_input_path, 'wb') as f:
142
- f.write(uploaded_file.getvalue())
143
-
144
- # Handle PDF conversion if necessary
145
- file_extension = Path(uploaded_file.name).suffix.lower()
146
- if file_extension == '.pdf':
147
- st.info("Converting PDF to Excel format...")
148
- with st.spinner("This may take a moment..."):
149
- temp_excel_path = os.path.join(temp_dir, 'converted.xlsx')
150
- temp_excel_path = pdf_to_excel(temp_input_path, temp_excel_path)
151
- st.success("PDF converted to Excel successfully!")
152
- else:
153
- temp_excel_path = temp_input_path
154
-
155
- # Get sheet names
156
- sheet_names = get_excel_sheets(temp_excel_path)
157
-
158
- st.write("### File Details")
159
- col1, col2 = st.columns(2)
160
- with col1:
161
- st.write(f"File name: {uploaded_file.name}")
162
- st.write(f"Number of sheets: {len(sheet_names)}")
163
-
164
- # Sheet selection
165
- st.write("### Sheet Selection")
166
- selected_sheets = st.multiselect(
167
- "Select sheets to convert (leave empty to convert all sheets)",
168
- options=sheet_names,
169
- default=None
170
- )
171
-
172
- if not selected_sheets:
173
- selected_sheets = None
174
- st.info("All sheets will be converted")
175
-
176
- # Database name input
177
- default_db_name = Path(uploaded_file.name).stem + ".db"
178
- db_name = st.text_input(
179
- "Enter database name",
180
- value=default_db_name,
181
- help="Enter the name for your SQLite database file"
182
- )
183
-
184
- # Convert button
185
- if st.button("Convert to Database"):
186
- try:
187
- with st.spinner("Converting to SQLite database..."):
188
- # Create database in temporary directory
189
- temp_db_path = os.path.join(temp_dir, db_name)
190
-
191
- # Process the conversion
192
- db_path, conversion_info = excel_to_database(
193
- temp_excel_path,
194
- temp_db_path,
195
- selected_sheets
196
- )
197
-
198
- # Display conversion results
199
- st.success("Conversion completed successfully!")
200
-
201
- # If it was a PDF, offer Excel download too
202
- if file_extension == '.pdf':
203
- with open(temp_excel_path, 'rb') as f:
204
- st.download_button(
205
- label="Download Converted Excel",
206
- data=f.read(),
207
- file_name="converted.xlsx",
208
- mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
209
- help="Download the intermediate Excel file"
210
- )
211
-
212
- st.write("### Conversion Details")
213
- for info in conversion_info:
214
- with st.expander(f"Sheet: {info['sheet_name']}"):
215
- st.write(f"Table name: {info['table_name']}")
216
- st.write(f"Number of rows: {info['rows']}")
217
- st.write(f"Columns: {', '.join(info['columns'])}")
218
-
219
- # Show data preview
220
- st.write("Data Preview:")
221
- preview_df = get_table_preview(temp_db_path, info['table_name'])
222
- st.dataframe(preview_df)
223
-
224
- # Provide download link for database
225
- with open(temp_db_path, 'rb') as f:
226
- st.download_button(
227
- label="Download SQLite Database",
228
- data=f.read(),
229
- file_name=db_name,
230
- mime="application/x-sqlite3",
231
- help="Click to download the converted database file"
232
- )
233
-
234
- except Exception as e:
235
- st.error(f"Error during conversion: {str(e)}")
236
- st.write("Please check your file and try again.")
237
-
238
- except Exception as e:
239
- st.error(f"Error processing file: {str(e)}")
240
-
241
- finally:
242
- # Clean up temporary directory
243
- try:
244
- shutil.rmtree(temp_dir)
245
- except Exception:
246
- pass
247
-
248
- if __name__ == "__main__":
249
- main()