imranalishah commited on
Commit
f7db5f7
·
verified ·
1 Parent(s): 12e8e5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -20
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
- header_written = False
 
12
 
13
  for sheet_name in wb.sheetnames:
14
  ws = wb[sheet_name]
15
 
16
- # Extract employee ID from row 8, column G (index 6)
17
- emp_id_cell = ws.cell(row=8, column=7) # 1-based index
18
- employee_id = emp_id_cell.value if emp_id_cell.value else "Unknown"
 
 
 
19
 
20
- # Get headers from row 11
21
- header_row = [cell.value for cell in ws[11]]
22
- if not header_written:
23
- result_ws.append(["Employee ID", "Sheet Name"] + header_row)
24
- header_written = True
 
25
 
26
- # Find index of Remarks column
27
- try:
28
- remarks_idx = header_row.index("Remarks")
29
- except ValueError:
30
- continue # Skip this sheet if no Remarks column
31
 
32
- # Iterate from row 12 onward
33
- for row in ws.iter_rows(min_row=12, values_only=True):
34
- remarks = row[remarks_idx]
35
- if remarks and "official visit" in str(remarks).lower():
36
- result_ws.append([employee_id, sheet_name] + list(row))
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. It extracts rows where 'Remarks' contains 'Official visit', adding the correct Employee ID from sheet metadata."
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__":