Krish30's picture
Upload app.py
52abe54 verified
# V5 engg upload
import streamlit as st
import pandas as pd
import tabula
import os
from io import BytesIO
# Engineering Result Type 1 Functions
def extract_engineering_result(pdf_path):
try:
df = tabula.read_pdf(pdf_path, pages='all', multiple_tables=True)
return df
except Exception as e:
st.error(f"Error extracting data from Engineering PDF: {e}")
return None
# HSC Result Function
def extract_hsc_result(pdf_path):
try:
df = tabula.read_pdf(pdf_path, pages='all')
return df
except Exception as e:
st.error(f"Error extracting data from HSC PDF: {e}")
return None
# Diploma Result Function
def extract_diploma_result(pdf_path):
try:
df = tabula.read_pdf(pdf_path, pages='all')
return df
except Exception as e:
st.error(f"Error extracting data from Diploma PDF: {e}")
return None
# Streamlit App
def main():
st.title("PDF Result Converter")
# File Upload
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
if uploaded_file is not None:
file_details = {"FileName": uploaded_file.name, "FileType": uploaded_file.type}
st.write(file_details)
# Determine which type of PDF and call the appropriate extraction function
if "engineering" in uploaded_file.name.lower() or "engg" in uploaded_file.name.lower():
extracted_data = extract_engineering_result(uploaded_file)
elif "hsc" in uploaded_file.name.lower():
extracted_data = extract_hsc_result(uploaded_file)
elif "diploma" in uploaded_file.name.lower():
extracted_data = extract_diploma_result(uploaded_file)
else:
st.error("Unsupported PDF type. Please upload a valid PDF.")
return
# Concatenate all extracted DataFrames into a single DataFrame
if extracted_data is not None and isinstance(extracted_data, list):
combined_df = pd.concat(extracted_data, ignore_index=True)
elif extracted_data is not None and isinstance(extracted_data, pd.DataFrame):
combined_df = extracted_data
else:
st.error("No data extracted or extraction failed. Please check the PDF file and extraction logic.")
return
# Display the extracted data (for debugging purposes)
st.subheader("Combined Extracted Data:")
st.write(combined_df)
# Convert to Excel and create download link
if st.button("Convert to Excel"):
output = BytesIO()
excel_writer = pd.ExcelWriter(output, engine='xlsxwriter')
combined_df.to_excel(excel_writer, index=False, sheet_name='Sheet1')
excel_writer.close()
excel_data = output.getvalue()
output.seek(0)
# Provide a download button for the generated Excel file
st.download_button(
label="Download Excel File",
data=excel_data,
file_name=f"{uploaded_file.name.split('.')[0]}.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
key="download_excel"
)
if __name__ == "__main__":
main()