Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,41 +5,48 @@ from tempfile import NamedTemporaryFile
|
|
| 5 |
def process_excel(file):
|
| 6 |
wb = openpyxl.load_workbook(file, data_only=True)
|
| 7 |
|
| 8 |
-
# Create
|
| 9 |
result_wb = openpyxl.Workbook()
|
| 10 |
result_ws = result_wb.active
|
| 11 |
result_ws.title = "Official Visits"
|
| 12 |
|
| 13 |
-
|
| 14 |
-
result_ws.append(["Employee ID", "Sheet", "Row Data..."])
|
| 15 |
|
| 16 |
for sheet_name in wb.sheetnames:
|
| 17 |
ws = wb[sheet_name]
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# Find the header row and locate 'Remarks' column
|
| 23 |
-
remarks_col_idx = None
|
| 24 |
-
header_row_idx = None
|
| 25 |
-
|
| 26 |
for row in ws.iter_rows(min_row=1, max_row=10):
|
| 27 |
-
|
| 28 |
-
if
|
| 29 |
-
|
| 30 |
-
header_row_idx = row[0].row
|
| 31 |
break
|
| 32 |
|
| 33 |
-
if
|
| 34 |
-
continue # Skip
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
if remarks_cell.value and "official visit" in str(remarks_cell.value).lower():
|
|
|
|
| 39 |
row_values = [cell.value for cell in row]
|
| 40 |
-
result_ws.append([
|
| 41 |
|
| 42 |
-
# Save to
|
| 43 |
with NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
| 44 |
result_wb.save(tmp.name)
|
| 45 |
return tmp.name
|
|
@@ -47,11 +54,4 @@ def process_excel(file):
|
|
| 47 |
# Gradio interface
|
| 48 |
iface = gr.Interface(
|
| 49 |
fn=process_excel,
|
| 50 |
-
inputs=gr.File(
|
| 51 |
-
outputs=gr.File(label="Download Official Visit Report"),
|
| 52 |
-
title="Official Visit Extractor",
|
| 53 |
-
description="Upload an Excel file with multiple sheets. This tool finds rows where 'Remarks' contain 'Official visit' and compiles them."
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
if __name__ == "__main__":
|
| 57 |
-
iface.launch()
|
|
|
|
| 5 |
def process_excel(file):
|
| 6 |
wb = openpyxl.load_workbook(file, data_only=True)
|
| 7 |
|
| 8 |
+
# Create output workbook
|
| 9 |
result_wb = openpyxl.Workbook()
|
| 10 |
result_ws = result_wb.active
|
| 11 |
result_ws.title = "Official Visits"
|
| 12 |
|
| 13 |
+
header_written = False
|
|
|
|
| 14 |
|
| 15 |
for sheet_name in wb.sheetnames:
|
| 16 |
ws = wb[sheet_name]
|
| 17 |
|
| 18 |
+
# Detect header row (assume first non-empty row within top 10 rows)
|
| 19 |
+
header_row = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
for row in ws.iter_rows(min_row=1, max_row=10):
|
| 21 |
+
values = [cell.value for cell in row]
|
| 22 |
+
if any(values):
|
| 23 |
+
header_row = row
|
|
|
|
| 24 |
break
|
| 25 |
|
| 26 |
+
if not header_row:
|
| 27 |
+
continue # Skip if no header found
|
| 28 |
+
|
| 29 |
+
headers = [str(cell.value).strip() if cell.value else "" for cell in header_row]
|
| 30 |
+
try:
|
| 31 |
+
remarks_idx = headers.index("Remarks")
|
| 32 |
+
emp_id_idx = headers.index("Employee ID")
|
| 33 |
+
except ValueError:
|
| 34 |
+
continue # Skip if required columns not found
|
| 35 |
+
|
| 36 |
+
# Write header once
|
| 37 |
+
if not header_written:
|
| 38 |
+
result_ws.append(["Employee ID", "Sheet Name"] + headers)
|
| 39 |
+
header_written = True
|
| 40 |
|
| 41 |
+
# Iterate through data rows
|
| 42 |
+
for row in ws.iter_rows(min_row=header_row[0].row + 1):
|
| 43 |
+
remarks_cell = row[remarks_idx]
|
| 44 |
if remarks_cell.value and "official visit" in str(remarks_cell.value).lower():
|
| 45 |
+
emp_id_value = row[emp_id_idx].value
|
| 46 |
row_values = [cell.value for cell in row]
|
| 47 |
+
result_ws.append([emp_id_value, sheet_name] + row_values)
|
| 48 |
|
| 49 |
+
# Save the result workbook to temp file
|
| 50 |
with NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
| 51 |
result_wb.save(tmp.name)
|
| 52 |
return tmp.name
|
|
|
|
| 54 |
# Gradio interface
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=process_excel,
|
| 57 |
+
inputs=gr.File(file_types_
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|