timfocus commited on
Commit
a3a8092
·
verified ·
1 Parent(s): a919f44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -26
app.py CHANGED
@@ -3,6 +3,9 @@ import pandas as pd
3
  import gradio as gr
4
  from reportlab.pdfgen import canvas
5
  from reportlab.lib.pagesizes import letter
 
 
 
6
  import barcode
7
  from barcode.writer import ImageWriter
8
 
@@ -12,13 +15,6 @@ BARCODES_DIR = "barcodes"
12
  os.makedirs(LABELS_DIR, exist_ok=True)
13
  os.makedirs(BARCODES_DIR, exist_ok=True)
14
 
15
- # Required columns for the input file
16
- REQUIRED_COLUMNS = [
17
- "Order ID", "Order Date", "Shipper Name", "Shipper Address", "Shipper Phone",
18
- "Receiver Name", "Receiver Address", "Receiver Phone", "Package Weight (kg)",
19
- "Shipping Method", "Tracking Number", "Estimated Delivery Date"
20
- ]
21
-
22
  # Function to process the uploaded file
23
  def process_file(file_path):
24
  try:
@@ -30,8 +26,14 @@ def process_file(file_path):
30
  else:
31
  return "Invalid file format. Please upload a .csv, .xls, or .xlsx file."
32
 
33
- # Ensure all required columns exist
34
- missing_cols = [col for col in REQUIRED_COLUMNS if col not in df.columns]
 
 
 
 
 
 
35
  if missing_cols:
36
  return f"Missing columns: {', '.join(missing_cols)}"
37
 
@@ -47,7 +49,7 @@ def process_file(file_path):
47
  except Exception as e:
48
  return f"Error processing file: {str(e)}"
49
 
50
- # Function to create a shipping label
51
  def create_shipping_label(row, index):
52
  try:
53
  tracking_number = str(row["Tracking Number"])
@@ -62,27 +64,58 @@ def create_shipping_label(row, index):
62
  label_path = os.path.join(LABELS_DIR, f"label_{index}.pdf")
63
  c = canvas.Canvas(label_path, pagesize=letter)
64
 
65
- # Add shipping details
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  c.setFont("Helvetica", 12)
67
- c.drawString(50, 750, f"Order ID: {row['Order ID']}")
68
- c.drawString(50, 730, f"Date: {row['Order Date']}")
69
- c.drawString(50, 710, f"From: {row['Shipper Name']}")
70
- c.drawString(50, 690, f"Address: {row['Shipper Address']}")
71
- c.drawString(50, 670, f"Phone: {row['Shipper Phone']}")
72
- c.drawString(50, 650, f"To: {row['Receiver Name']}")
73
- c.drawString(50, 630, f"Address: {row['Receiver Address']}")
74
- c.drawString(50, 610, f"Phone: {row['Receiver Phone']}")
75
- c.drawString(50, 590, f"Weight: {row['Package Weight (kg)']} kg")
76
- c.drawString(50, 570, f"Method: {row['Shipping Method']}")
77
- c.drawString(50, 550, f"Tracking #: {row['Tracking Number']}")
78
- c.drawString(50, 530, f"ETA: {row['Estimated Delivery Date']}")
79
 
80
  # Add barcode
 
 
81
  if os.path.exists(barcode_path):
82
- c.drawImage(barcode_path, 50, 400, width=200, height=50)
83
  else:
84
  c.drawString(50, 400, "Barcode generation failed.")
85
 
 
 
 
 
 
86
  c.showPage()
87
  c.save()
88
 
@@ -93,10 +126,10 @@ def create_shipping_label(row, index):
93
  # Gradio interface
94
  iface = gr.Interface(
95
  fn=process_file,
96
- inputs=gr.File(type="filepath"), # FIXED: Use 'filepath' instead of 'file'
97
  outputs=gr.File(type="filepath", label="Download Labels"),
98
  title="Shipping Label Generator",
99
- description="Upload a .csv, .xls, or .xlsx file to generate shipping labels."
100
  )
101
 
102
  iface.launch()
 
3
  import gradio as gr
4
  from reportlab.pdfgen import canvas
5
  from reportlab.lib.pagesizes import letter
6
+ from reportlab.lib import colors
7
+ from reportlab.lib.utils import ImageReader
8
+ from reportlab.lib.styles import getSampleStyleSheet
9
  import barcode
10
  from barcode.writer import ImageWriter
11
 
 
15
  os.makedirs(LABELS_DIR, exist_ok=True)
16
  os.makedirs(BARCODES_DIR, exist_ok=True)
17
 
 
 
 
 
 
 
 
18
  # Function to process the uploaded file
19
  def process_file(file_path):
20
  try:
 
26
  else:
27
  return "Invalid file format. Please upload a .csv, .xls, or .xlsx file."
28
 
29
+ # Ensure required columns exist
30
+ required_cols = [
31
+ "Order ID", "Order Date", "Shipper Name", "Shipper Address", "Shipper Phone",
32
+ "Receiver Name", "Receiver Address", "Receiver Phone", "Package Weight (kg)",
33
+ "Shipping Method", "Tracking Number", "Estimated Delivery Date"
34
+ ]
35
+
36
+ missing_cols = [col for col in required_cols if col not in df.columns]
37
  if missing_cols:
38
  return f"Missing columns: {', '.join(missing_cols)}"
39
 
 
49
  except Exception as e:
50
  return f"Error processing file: {str(e)}"
51
 
52
+ # Function to create a USPS-style shipping label
53
  def create_shipping_label(row, index):
54
  try:
55
  tracking_number = str(row["Tracking Number"])
 
64
  label_path = os.path.join(LABELS_DIR, f"label_{index}.pdf")
65
  c = canvas.Canvas(label_path, pagesize=letter)
66
 
67
+ # Header: USPS branding and tracking info
68
+ c.setFont("Helvetica-Bold", 20)
69
+ c.drawString(50, 750, "USPS FIRST-CLASS PKG")
70
+ c.setFont("Helvetica", 12)
71
+ c.drawString(50, 730, f"Order ID: {row['Order ID']} | Date: {row['Order Date']}")
72
+
73
+ # Sender information
74
+ c.setFont("Helvetica-Bold", 12)
75
+ c.drawString(50, 700, "From:")
76
+ c.setFont("Helvetica", 12)
77
+ c.drawString(50, 685, f"{row['Shipper Name']}")
78
+ c.drawString(50, 670, f"{row['Shipper Address']}")
79
+ c.drawString(50, 655, f"Phone: {row['Shipper Phone']}")
80
+
81
+ # Receiver information
82
+ c.setFont("Helvetica-Bold", 12)
83
+ c.drawString(50, 620, "To:")
84
+ c.setFont("Helvetica-Bold", 14)
85
+ c.drawString(50, 605, row["Receiver Name"]) # Bold receiver name
86
+ c.setFont("Helvetica", 12)
87
+ c.drawString(50, 590, row["Receiver Address"])
88
+ c.drawString(50, 575, f"Phone: {row['Receiver Phone']}")
89
+
90
+ # Shipping details
91
+ c.setFont("Helvetica-Bold", 12)
92
+ c.drawString(50, 540, "Shipping Method:")
93
  c.setFont("Helvetica", 12)
94
+ c.drawString(50, 525, row["Shipping Method"])
95
+
96
+ c.setFont("Helvetica-Bold", 12)
97
+ c.drawString(250, 540, "Package Weight:")
98
+ c.setFont("Helvetica", 12)
99
+ c.drawString(250, 525, f"{row['Package Weight (kg)']} kg")
100
+
101
+ c.setFont("Helvetica-Bold", 12)
102
+ c.drawString(50, 500, "Estimated Delivery:")
103
+ c.setFont("Helvetica", 12)
104
+ c.drawString(50, 485, row["Estimated Delivery Date"])
 
105
 
106
  # Add barcode
107
+ c.setFont("Helvetica-Bold", 14)
108
+ c.drawString(50, 450, "USPS TRACKING #")
109
  if os.path.exists(barcode_path):
110
+ c.drawImage(barcode_path, 50, 400, width=300, height=70)
111
  else:
112
  c.drawString(50, 400, "Barcode generation failed.")
113
 
114
+ # Tracking number below barcode
115
+ c.setFont("Helvetica-Bold", 12)
116
+ c.drawString(50, 370, tracking_number)
117
+
118
+ # Finalize PDF
119
  c.showPage()
120
  c.save()
121
 
 
126
  # Gradio interface
127
  iface = gr.Interface(
128
  fn=process_file,
129
+ inputs=gr.File(type="filepath"),
130
  outputs=gr.File(type="filepath", label="Download Labels"),
131
  title="Shipping Label Generator",
132
+ description="Upload a .csv, .xls, or .xlsx file to generate USPS-style shipping labels."
133
  )
134
 
135
  iface.launch()