Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,48 +4,53 @@ from tempfile import NamedTemporaryFile
|
|
| 4 |
|
| 5 |
def process_excel(file):
|
| 6 |
wb = openpyxl.load_workbook(file, data_only=True)
|
|
|
|
|
|
|
| 7 |
result_wb = openpyxl.Workbook()
|
| 8 |
result_ws = result_wb.active
|
| 9 |
result_ws.title = "Official Visits"
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
for sheet_name in wb.sheetnames:
|
| 14 |
ws = wb[sheet_name]
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
remarks_idx = header_row.index("Remarks")
|
| 29 |
-
except ValueError:
|
| 30 |
-
continue # Skip this sheet if no Remarks column
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
result_ws.append([
|
| 37 |
|
| 38 |
# Save to temporary file
|
| 39 |
with NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
| 40 |
result_wb.save(tmp.name)
|
| 41 |
return tmp.name
|
| 42 |
|
|
|
|
| 43 |
iface = gr.Interface(
|
| 44 |
fn=process_excel,
|
| 45 |
inputs=gr.File(file_types=[".xlsx"]),
|
| 46 |
outputs=gr.File(label="Download Official Visit Report"),
|
| 47 |
title="Official Visit Extractor",
|
| 48 |
-
description="Upload an Excel file with multiple sheets.
|
| 49 |
)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
|
|
|
| 4 |
|
| 5 |
def process_excel(file):
|
| 6 |
wb = openpyxl.load_workbook(file, data_only=True)
|
| 7 |
+
|
| 8 |
+
# Create a new workbook for the result
|
| 9 |
result_wb = openpyxl.Workbook()
|
| 10 |
result_ws = result_wb.active
|
| 11 |
result_ws.title = "Official Visits"
|
| 12 |
|
| 13 |
+
# Header row
|
| 14 |
+
result_ws.append(["Employee ID (from G7)", "Sheet", "Row Data..."])
|
| 15 |
|
| 16 |
for sheet_name in wb.sheetnames:
|
| 17 |
ws = wb[sheet_name]
|
| 18 |
|
| 19 |
+
# Get employee ID from G7
|
| 20 |
+
emp_id = ws["G7"].value or "Unknown"
|
| 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 |
+
headers = [str(cell.value).strip().lower() if cell.value else "" for cell in row]
|
| 28 |
+
if "remarks" in headers:
|
| 29 |
+
remarks_col_idx = headers.index("remarks")
|
| 30 |
+
header_row_idx = row[0].row
|
| 31 |
+
break
|
| 32 |
|
| 33 |
+
if remarks_col_idx is None:
|
| 34 |
+
continue # Skip this sheet if no 'Remarks' column found
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
for row in ws.iter_rows(min_row=header_row_idx + 1):
|
| 37 |
+
remarks_cell = row[remarks_col_idx]
|
| 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([emp_id, sheet_name] + row_values)
|
| 41 |
|
| 42 |
# Save to temporary file
|
| 43 |
with NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
| 44 |
result_wb.save(tmp.name)
|
| 45 |
return tmp.name
|
| 46 |
|
| 47 |
+
# Gradio interface
|
| 48 |
iface = gr.Interface(
|
| 49 |
fn=process_excel,
|
| 50 |
inputs=gr.File(file_types=[".xlsx"]),
|
| 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. Employee ID is taken from cell G7 of each sheet."
|
| 54 |
)
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|