From 06236417273642775980504f228474058b890c84 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 4 Dec 2024 13:30:41 +0500 Subject: [PATCH] 8 test --- test_apps/test_app8.py | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 test_apps/test_app8.py diff --git a/test_apps/test_app8.py b/test_apps/test_app8.py new file mode 100644 index 0000000..2f04f4c --- /dev/null +++ b/test_apps/test_app8.py @@ -0,0 +1,78 @@ +import pandas as pd +from flask import Flask, request, render_template_string +import os + +app = Flask(__name__) + +# Directory to store converted CSV files +UPLOAD_FOLDER = 'converted_files' +os.makedirs(UPLOAD_FOLDER, exist_ok=True) + +@app.route('/') +def index(): + return ''' +

Upload Excel File

+
+ +

+ +

+ +
+ ''' + +@app.route('/upload', methods=['POST']) +def upload_file(): + if 'file' not in request.files or 'tracking_no' not in request.form: + return "No file or tracking number provided.", 400 + + file = request.files['file'] + tracking_no = request.form['tracking_no'] + filename = file.filename + + if not filename.endswith(('.xls', '.xlsx')): + return "Unsupported file type. Please upload an Excel file.", 400 + + try: + # Read all sheets from the Excel file with no header + file.seek(0) # Reset file pointer + sheet_data = pd.read_excel(file, sheet_name=None, header=None, dtype=str) + + result_html = "

Tracking Information

" + + # Flag to track if the tracking number is found + tracking_found = False + + # Process each sheet + for sheet_name, df in sheet_data.items(): + # Iterate through every row and column to search for the tracking number + for row_idx, row in df.iterrows(): + for col_idx, cell in row.items(): + # Convert cell to string before searching + cell_str = str(cell) + + # Check if the tracking number exists in the current cell + if tracking_no.lower() in cell_str.lower(): + tracking_found = True + # Adjusting the row and column for user-friendly display (starting from 1) + adjusted_row_idx = row_idx + 1 + adjusted_col_idx = col_idx + 1 + result_html += f"

Tracking No: {tracking_no} found in sheet '{sheet_name}'

" + result_html += f"

Row {adjusted_row_idx}, Column {adjusted_col_idx}: {cell_str}

" + + # Once the tracking number is found, no need to process further sheets + if tracking_found: + break + + # If no tracking number was found, display a message + if not tracking_found: + result_html += f"

No data found for Tracking No: {tracking_no}

" + + return render_template_string(result_html) + + except Exception as e: + return f"Error processing file: {e}", 500 + + +if __name__ == '__main__': + app.run(debug=True, port=5000, host='0.0.0.0')