sri8888 commited on
Commit
8cc7fbc
·
verified ·
1 Parent(s): 4a26794

Create excel.py

Browse files
Files changed (1) hide show
  1. excel.py +54 -0
excel.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import re
3
+ import gradio as gr
4
+
5
+ # Function to check if a cell contains a number with matching last 4 digits
6
+ def contains_matching_number(row, numbers_list):
7
+ for item in row:
8
+ item_str = str(item) # Convert cell value to string
9
+ for number in numbers_list:
10
+ if re.search(fr"\b{number}\b", item_str[-4:]): # Match last 4 digits
11
+ return True
12
+ return False
13
+
14
+ # Function to process the Excel file
15
+ def process_excel(file, numbers):
16
+ # Read the Excel file
17
+ df = pd.read_excel(file, sheet_name=0) # Read the first sheet
18
+
19
+ # Convert input text to a list of numbers
20
+ numbers_list = numbers.split(",") # Example: "10857, 2140" → ['10857', '2140']
21
+ numbers_list = [num.strip()[-4:] for num in numbers_list] # Extract last 4 digits
22
+
23
+ # Filter rows where any cell contains a number matching last 4 digits
24
+ matched_rows = df[df.apply(lambda row: contains_matching_number(row, numbers_list), axis=1)]
25
+
26
+ # Save results to a new Excel file
27
+ output_file = "processed_data.xlsx"
28
+ with pd.ExcelWriter(output_file, engine="openpyxl") as writer:
29
+ df.to_excel(writer, sheet_name="OriginalData", index=False)
30
+ matched_rows.to_excel(writer, sheet_name="MatchedRows", index=False)
31
+
32
+ return output_file
33
+
34
+ # Gradio Interface
35
+ def gradio_interface(file, numbers):
36
+ if file is None:
37
+ return None
38
+ processed_file = process_excel(file.name, numbers)
39
+ return processed_file
40
+
41
+ # Create Gradio UI
42
+ iface = gr.Interface(
43
+ fn=gradio_interface,
44
+ inputs=[
45
+ gr.File(label="Upload Excel File"),
46
+ gr.Textbox(label="Enter numbers to match (comma-separated)")
47
+ ],
48
+ outputs=gr.File(label="Download Processed Excel File"),
49
+ title="Excel Row Matcher (Last 4 Digits)",
50
+ description="Upload an Excel file and enter numbers. The script will find rows where any cell contains the last 4 digits matching the entered numbers and save them to a new sheet.",
51
+ )
52
+
53
+ # Launch the Gradio app
54
+ iface.launch(share=True)