AO, cit, itat report download code commit.
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
from AppCode.Config import DBConfig
|
||||
import mysql.connector
|
||||
import pandas as pd
|
||||
import io
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -91,3 +94,43 @@ class AOHandler:
|
||||
def close(self):
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
||||
|
||||
def ao_report_download(self, selected_year):
|
||||
try:
|
||||
# Call stored proc to fetch year-wise records
|
||||
self.cursor.callproc("GetAOByYear", [selected_year])
|
||||
|
||||
rows = []
|
||||
for result in self.cursor.stored_results():
|
||||
rows = result.fetchall()
|
||||
|
||||
if not rows:
|
||||
return None
|
||||
|
||||
df = pd.DataFrame(rows)
|
||||
|
||||
# TRANSPOSE
|
||||
df_transposed = df.transpose()
|
||||
df_transposed.insert(0, 'Field', df_transposed.index)
|
||||
|
||||
# Rename columns: Record 1, 2, 3...
|
||||
record_cols = {
|
||||
i: f"Record {i}"
|
||||
for i in df_transposed.columns if isinstance(i, int)
|
||||
}
|
||||
df_transposed.rename(columns=record_cols, inplace=True)
|
||||
df_transposed.reset_index(drop=True, inplace=True)
|
||||
|
||||
# Excel Output
|
||||
output = io.BytesIO()
|
||||
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
||||
df_transposed.to_excel(writer, index=False, sheet_name="AO_Vertical")
|
||||
worksheet = writer.sheets["AO_Vertical"]
|
||||
worksheet.set_column(0, 0, 30)
|
||||
|
||||
output.seek(0)
|
||||
return output
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print("MySQL Error:", e)
|
||||
return None
|
||||
Reference in New Issue
Block a user