import pandas as pd
from flask import Flask, request, render_template_string
app = Flask(__name__)
@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']
try:
# Load the CSV with no header since we need to manually locate key data points
df = pd.read_csv(file, header=None, dtype=str)
# Find the "Mail Bag No:" label and retrieve its value from column C (index 2)
mail_bag_no = None
for i, row in df.iterrows():
if row[0] == "Mail Bag No:":
mail_bag_no = row[2]
break
# If "Mail Bag No:" is not found, set it to "N/A"
if mail_bag_no is None:
mail_bag_no = "N/A"
# Locate header row by looking for "Tracking No" in any row
header_row_index = None
for i, row in df.iterrows():
if row.str.contains("Tracking No", case=False, na=False).any():
header_row_index = i
break
if header_row_index is None:
return "'Tracking No' column not found in the CSV file."
# Reload the CSV with the identified header row
file.seek(0) # Reset file pointer to start of file for re-read
df = pd.read_csv(file, skiprows=header_row_index)
except Exception as e:
return f"Error reading CSV file: {e}", 500
# Confirm the 'Tracking No' column exists in this adjusted DataFrame
if 'Tracking No' not in df.columns:
return f"'Tracking No' column not found. Available columns after adjustment: {', '.join(df.columns)}"
# Filter the data based on the tracking number
filtered_data = df[df['Tracking No'] == tracking_no]
if filtered_data.empty:
return f"No data found for Tracking No: {tracking_no}"
# Collect data for each matching entry
result_html = "Tracking Information
"
for _, row in filtered_data.iterrows():
hash_column = row.get('#', "N/A")
result_html += f"""
Tracking No: {row['Tracking No']}
Mail Bag No: {mail_bag_no}
#: {hash_column}
"""
return render_template_string(result_html)
if __name__ == '__main__':
app.run(debug=True, port=5000, host='0.0.0.0')