File size: 3,252 Bytes
52abe54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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()