Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openpyxl
|
| 3 |
+
from tempfile import NamedTemporaryFile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def process_excel(file):
|
| 7 |
+
# Load the uploaded Excel file
|
| 8 |
+
wb = openpyxl.load_workbook(file)
|
| 9 |
+
|
| 10 |
+
# Create a new workbook for results
|
| 11 |
+
result_wb = openpyxl.Workbook()
|
| 12 |
+
result_ws = result_wb.active
|
| 13 |
+
result_ws.title = "Official Visits"
|
| 14 |
+
result_ws.append(["Employee ID"] + ["Data..."]) # Placeholder header
|
| 15 |
+
|
| 16 |
+
for sheet_name in wb.sheetnames:
|
| 17 |
+
if sheet_name == 'Official Visits':
|
| 18 |
+
continue
|
| 19 |
+
|
| 20 |
+
ws = wb[sheet_name]
|
| 21 |
+
employee_id = ws.cell(row=1, column=1).value # Assuming A1 has Employee ID
|
| 22 |
+
|
| 23 |
+
# Get headers (assume they are in row 2)
|
| 24 |
+
headers = [cell.value for cell in ws[2]]
|
| 25 |
+
try:
|
| 26 |
+
remarks_col_idx = headers.index("Remarks") + 1 # openpyxl is 1-based
|
| 27 |
+
except ValueError:
|
| 28 |
+
continue
|
| 29 |
+
|
| 30 |
+
for row in ws.iter_rows(min_row=3):
|
| 31 |
+
cell = row[remarks_col_idx - 1]
|
| 32 |
+
if cell.value and "official visit" in str(cell.value).lower():
|
| 33 |
+
row_data = [cell.value for cell in row]
|
| 34 |
+
result_ws.append([employee_id] + row_data)
|
| 35 |
+
|
| 36 |
+
# Save to a temporary file
|
| 37 |
+
with NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
| 38 |
+
result_wb.save(tmp.name)
|
| 39 |
+
return tmp.name # Return path to the saved Excel file
|
| 40 |
+
|
| 41 |
+
# Gradio interface
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=process_excel,
|
| 44 |
+
inputs=gr.File(file_types=[".xlsx"]),
|
| 45 |
+
outputs=gr.File(label="Download Processed Excel"),
|
| 46 |
+
title="Official Visit Extractor",
|
| 47 |
+
description="Upload an Excel file with multiple sheets. This app extracts all rows where 'Remarks' contain 'Official visit'."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
iface.launch()
|