imranalishah's picture
Update app.py
f7db5f7 verified
Raw History Blame Contribute Delete
2.01 kB
import gradio as gr
import openpyxl
from tempfile import NamedTemporaryFile
def process_excel(file):
wb = openpyxl.load_workbook(file, data_only=True)
# Create a new workbook for the result
result_wb = openpyxl.Workbook()
result_ws = result_wb.active
result_ws.title = "Official Visits"
# Header row
result_ws.append(["Employee ID (from G7)", "Sheet", "Row Data..."])
for sheet_name in wb.sheetnames:
ws = wb[sheet_name]
# Get employee ID from G7
emp_id = ws["G7"].value or "Unknown"
# Find the header row and locate 'Remarks' column
remarks_col_idx = None
header_row_idx = None
for row in ws.iter_rows(min_row=1, max_row=10):
headers = [str(cell.value).strip().lower() if cell.value else "" for cell in row]
if "remarks" in headers:
remarks_col_idx = headers.index("remarks")
header_row_idx = row[0].row
break
if remarks_col_idx is None:
continue # Skip this sheet if no 'Remarks' column found
for row in ws.iter_rows(min_row=header_row_idx + 1):
remarks_cell = row[remarks_col_idx]
if remarks_cell.value and "official visit" in str(remarks_cell.value).lower():
row_values = [cell.value for cell in row]
result_ws.append([emp_id, sheet_name] + row_values)
# Save to temporary file
with NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
result_wb.save(tmp.name)
return tmp.name
# Gradio interface
iface = gr.Interface(
fn=process_excel,
inputs=gr.File(file_types=[".xlsx"]),
outputs=gr.File(label="Download Official Visit Report"),
title="Official Visit Extractor",
description="Upload an Excel file with multiple sheets. This tool finds rows where 'Remarks' contain 'Official visit' and compiles them. Employee ID is taken from cell G7 of each sheet."
)
if __name__ == "__main__":
iface.launch()