Spaces:
Sleeping
Sleeping
Upload trend_report.py
Browse files- trend_report.py +81 -0
trend_report.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import io
|
| 3 |
+
from openpyxl.styles import Alignment, Font, Border, Side, PatternFill
|
| 4 |
+
|
| 5 |
+
def generate_trend_report_excel(df1, df2):
|
| 6 |
+
"""
|
| 7 |
+
df1: DataFrame of Base Semester (columns: USN, Name, SGPA, Marks, Pass_Percent)
|
| 8 |
+
df2: DataFrame of Current Semester (columns: USN, Name, SGPA, Marks, Pass_Percent)
|
| 9 |
+
"""
|
| 10 |
+
# Merge on USN
|
| 11 |
+
df_trend = pd.merge(df1, df2, on='USN', suffixes=('_Sem1', '_Sem2'))
|
| 12 |
+
|
| 13 |
+
if df_trend.empty:
|
| 14 |
+
output = io.BytesIO()
|
| 15 |
+
with pd.ExcelWriter(output, engine='openpyxl') as writer:
|
| 16 |
+
pd.DataFrame({'Message': ['No matching USNs found between the two datasets.']}).to_excel(writer, index=False)
|
| 17 |
+
output.seek(0)
|
| 18 |
+
return output
|
| 19 |
+
|
| 20 |
+
# Calculate Deltas
|
| 21 |
+
# SGPA
|
| 22 |
+
df_trend['SGPA_Sem1'] = pd.to_numeric(df_trend['SGPA_Sem1'], errors='coerce').fillna(0)
|
| 23 |
+
df_trend['SGPA_Sem2'] = pd.to_numeric(df_trend['SGPA_Sem2'], errors='coerce').fillna(0)
|
| 24 |
+
df_trend['SGPA_Delta'] = df_trend['SGPA_Sem2'] - df_trend['SGPA_Sem1']
|
| 25 |
+
|
| 26 |
+
# Identify Toppers/Improvers
|
| 27 |
+
df_trend['Status'] = df_trend['SGPA_Delta'].apply(lambda x: 'Improved' if x > 0 else ('Dropped' if x < 0 else 'Same'))
|
| 28 |
+
|
| 29 |
+
# Prepare for output
|
| 30 |
+
output_cols = ['USN', 'Name_Sem2', 'SGPA_Sem1', 'SGPA_Sem2', 'SGPA_Delta', 'Status']
|
| 31 |
+
df_out = df_trend[output_cols].copy()
|
| 32 |
+
df_out.rename(columns={'Name_Sem2': 'Name', 'SGPA_Sem1': 'Base SGPA', 'SGPA_Sem2': 'Current SGPA', 'SGPA_Delta': 'SGPA Change'}, inplace=True)
|
| 33 |
+
|
| 34 |
+
# Sort by biggest improvement
|
| 35 |
+
df_out = df_out.sort_values(by='SGPA Change', ascending=False)
|
| 36 |
+
|
| 37 |
+
output = io.BytesIO()
|
| 38 |
+
with pd.ExcelWriter(output, engine='openpyxl') as writer:
|
| 39 |
+
df_out.to_excel(writer, sheet_name='Student Trend Analysis', index=False)
|
| 40 |
+
ws = writer.sheets['Student Trend Analysis']
|
| 41 |
+
|
| 42 |
+
# Formatting
|
| 43 |
+
header_font = Font(bold=True, color="FFFFFF")
|
| 44 |
+
dark_fill = PatternFill(start_color="2F75B5", end_color="2F75B5", fill_type="solid")
|
| 45 |
+
green_fill = PatternFill(start_color="C6EFCE", end_color="C6EFCE", fill_type="solid")
|
| 46 |
+
red_fill = PatternFill(start_color="FFC7CE", end_color="FFC7CE", fill_type="solid")
|
| 47 |
+
thin_border = Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thin'))
|
| 48 |
+
|
| 49 |
+
# Header
|
| 50 |
+
for cell in ws[1]:
|
| 51 |
+
cell.font = header_font
|
| 52 |
+
cell.fill = dark_fill
|
| 53 |
+
cell.border = thin_border
|
| 54 |
+
|
| 55 |
+
# Rows
|
| 56 |
+
for row in ws.iter_rows(min_row=2):
|
| 57 |
+
for cell in row:
|
| 58 |
+
cell.border = thin_border
|
| 59 |
+
|
| 60 |
+
# Color Deltas
|
| 61 |
+
delta_cell = row[4] # SGPA Change
|
| 62 |
+
if delta_cell.value and isinstance(delta_cell.value, (int, float)):
|
| 63 |
+
if delta_cell.value > 0:
|
| 64 |
+
delta_cell.fill = green_fill
|
| 65 |
+
elif delta_cell.value < 0:
|
| 66 |
+
delta_cell.fill = red_fill
|
| 67 |
+
|
| 68 |
+
# Widths
|
| 69 |
+
from openpyxl.utils import get_column_letter
|
| 70 |
+
for col in ws.columns:
|
| 71 |
+
max_length = 0
|
| 72 |
+
column = get_column_letter(col[0].column)
|
| 73 |
+
for cell in col:
|
| 74 |
+
try:
|
| 75 |
+
if len(str(cell.value)) > max_length:
|
| 76 |
+
max_length = len(cell.value)
|
| 77 |
+
except: pass
|
| 78 |
+
ws.column_dimensions[column].width = min(max_length + 2, 50)
|
| 79 |
+
|
| 80 |
+
output.seek(0)
|
| 81 |
+
return output
|