import pandas as pd
import urllib.parse
import webbrowser
import os
def load_excel_data():
"""Load supplier data from the Excel file."""
try:
# Assuming the Excel file is in the same directory as the script
excel_path = os.path.join(os.path.dirname(__file__), "RM_Suppliers_List_Preparation_Draft_w_Notes - example.xlsx")
# Read the 'Main' sheet
suppliers_df = pd.read_excel(excel_path, sheet_name='Main')
print("Supplier data loaded successfully!")
return suppliers_df
except Exception as e:
print(f"Failed to load Excel file: {str(e)}")
exit()
def get_suppliers_for_material(suppliers_df, material_no):
"""Filter suppliers that produce the specified raw material."""
try:
# Filter suppliers that produce the requested material
filtered = suppliers_df[suppliers_df['RM Nr.'] == material_no]
# Group by supplier name and ID
grouped = filtered.groupby(['Supp. Name', 'Supp. ID']).first().reset_index()
return grouped.to_dict('records')
except Exception as e:
print(f"Error filtering suppliers: {str(e)}")
return []
def generate_emails(suppliers, material_no, total_tons, months, delivery_day):
"""Generate email drafts for selected suppliers."""
if not suppliers:
print("No suppliers selected.")
return
for supplier in suppliers:
subject = f"Request for Quotation - {material_no}"
body = f"""Dear {supplier['Supp. Name']},
We would like to request a quotation for the following raw material:
- Material Number: {material_no}
- Total Quantity Required: {total_tons} tons (over {months} months)
- Delivery Start Date: {delivery_day}
Please provide your best offer at your earliest convenience.
Best regards,
[Your Name]"""
# URL encode the subject and body
subject_encoded = urllib.parse.quote(subject)
body_encoded = urllib.parse.quote(body)
# Generate mailto link (assuming email is not in the data, using a placeholder)
mailto_link = f"mailto:example@example.com?subject={subject_encoded}&body={body_encoded}"
print(f"Generating email for {supplier['Supp. Name']}...")
webbrowser.open(mailto_link)
def main():
# Load supplier data
suppliers_df = load_excel_data()
# Get user input
material_no = input("Enter Raw Material Number: ").strip()
monthly_tons = input("Enter Monthly Needed Tons: ").strip()
months = input("Enter Number of Months: ").strip()
delivery_day = input("Enter Delivery Start Day: ").strip()
# Validate inputs
if not all([material_no, monthly_tons, months, delivery_day]):
print("Error: Please fill all fields.")
return
try:
monthly_tons = float(monthly_tons)
months = int(months)
total_tons = monthly_tons * months
except ValueError:
print("Error: Invalid numeric input.")
return
# Get suppliers for the specified material
suppliers = get_suppliers_for_material(suppliers_df, material_no)
if not suppliers:
print(f"No suppliers found for material {material_no}.")
return
# Display suppliers
print("\nSuppliers found:")
for idx, supplier in enumerate(suppliers):
print(f"{idx + 1}. {supplier['Supp. Name']} (ID: {supplier['Supp. ID']})")
# Select suppliers
selected_indices = input("\nEnter the numbers of the suppliers to contact (e.g., 1 3 5): ").strip().split()
try:
selected_indices = [int(idx) - 1 for idx in selected_indices]
selected_suppliers = [suppliers[i] for i in selected_indices]
except (ValueError, IndexError):
print("Error: Invalid selection.")
return
# Generate emails
generate_emails(selected_suppliers, material_no, total_tons, months, delivery_day)
print("Email generation complete.")
if __name__ == "__main__":
main()
sel = L.curselection()
lab.config(text=str(sel))
lab.pack()
F2.pack(side=Tkinter.TOP)
poll()
Tkinter.mainloop()