timfocus commited on
Commit
d8a38a8
·
verified ·
1 Parent(s): 273a8ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -13
app.py CHANGED
@@ -4,7 +4,6 @@ import gradio as gr
4
  import pandas as pd
5
  from reportlab.pdfgen import canvas
6
  from reportlab.lib.pagesizes import inch
7
- from reportlab.lib.utils import ImageReader
8
  import barcode
9
  from barcode.writer import ImageWriter
10
 
@@ -54,7 +53,7 @@ def draw_label_text(c, row, width, height):
54
  Overlays dynamic text from each row:
55
  Warehouse info, recipient address, tracking number, etc.
56
  """
57
- # Example columns from your data (adjust as needed)
58
  warehouse_line1 = row.get("Warehouse Line1", "WAREHOUSE 2")
59
  warehouse_line2 = row.get("Warehouse Line2", "11919 WINK RD")
60
  warehouse_line3 = row.get("Warehouse Line3", "HOUSTON TX 77024-7134")
@@ -65,7 +64,7 @@ def draw_label_text(c, row, width, height):
65
  recipient_addr2 = row.get("Recipient Addr2", "Salt Lake City, UT 11212")
66
  recipient_addr3 = row.get("Recipient Addr3", "United States")
67
 
68
- tracking_number = row.get("Tracking Number", "9400 1102 0079 3961 8936 91")
69
 
70
  # Warehouse info (top-left under the bar)
71
  c.setFont("Helvetica-Bold", 9)
@@ -75,6 +74,7 @@ def draw_label_text(c, row, width, height):
75
  c.drawString(15, height - 160, warehouse_line3)
76
 
77
  # Order number (top-right)
 
78
  c.drawString(width - 80, height - 130, f"Order: {order_number}")
79
 
80
  # Recipient info (bold name)
@@ -101,14 +101,14 @@ def create_usps_label(row, index=1):
101
  Creates a 4x6 inch PDF label using the collage background,
102
  then overlays dynamic text and a barcode at the bottom.
103
  """
104
- # 3A) Generate Code128 barcode
105
  tracking_number = row.get("Tracking Number", "9400110200793961893691")
106
  barcode_filename = os.path.join(BARCODES_DIR, f"barcode_{index}.png")
107
  code128 = barcode.get_barcode_class("code128")
108
  code128_obj = code128(tracking_number, writer=ImageWriter())
109
  code128_obj.save(barcode_filename)
110
 
111
- # 3B) Prepare a 4x6 inch PDF
112
  width, height = 4 * inch, 6 * inch
113
  label_filename = os.path.join(LABELS_DIR, f"label_{index}.pdf")
114
  c = canvas.Canvas(label_filename, pagesize=(width, height))
@@ -141,15 +141,15 @@ def process_file(file_obj):
141
  for f in os.listdir(BARCODES_DIR):
142
  os.remove(os.path.join(BARCODES_DIR, f))
143
 
144
- # Read the file
145
  if file_obj.name.endswith(".csv"):
146
  df = pd.read_csv(file_obj)
147
  elif file_obj.name.endswith((".xls", ".xlsx")):
148
  df = pd.read_excel(file_obj)
149
  else:
150
- return "Invalid file format. Please upload a .csv, .xls, or .xlsx file."
151
 
152
- # Basic check for columns
153
  needed_cols = [
154
  "Warehouse Line1", "Warehouse Line2", "Warehouse Line3", "Order Number",
155
  "Recipient Name", "Recipient Addr1", "Recipient Addr2", "Recipient Addr3",
@@ -157,23 +157,23 @@ def process_file(file_obj):
157
  ]
158
  missing = [c for c in needed_cols if c not in df.columns]
159
  if missing:
160
- return f"Missing columns: {', '.join(missing)}"
161
 
162
  # Generate a PDF label for each row
163
  for i, row in df.iterrows():
164
  create_usps_label(row.to_dict(), index=i+1)
165
 
166
- # Zip all PDFs
167
  zip_filename = "usps_labels.zip"
168
  with zipfile.ZipFile(zip_filename, "w") as zf:
169
  for f in os.listdir(LABELS_DIR):
170
  if f.endswith(".pdf"):
171
  zf.write(os.path.join(LABELS_DIR, f), f)
172
 
173
- return zip_filename
174
 
175
  except Exception as e:
176
- return f"Error processing file: {str(e)}"
177
 
178
  ############################################
179
  # 5) Gradio Interface
@@ -181,7 +181,7 @@ def process_file(file_obj):
181
  iface = gr.Interface(
182
  fn=process_file,
183
  inputs=gr.File(type="filepath"),
184
- outputs=gr.File(type="filepath", label="Download Zipped Labels"),
185
  title="USPS-Style Label Generator (4x6)",
186
  description=(
187
  "Upload a CSV/XLS/XLSX with columns:\n"
 
4
  import pandas as pd
5
  from reportlab.pdfgen import canvas
6
  from reportlab.lib.pagesizes import inch
 
7
  import barcode
8
  from barcode.writer import ImageWriter
9
 
 
53
  Overlays dynamic text from each row:
54
  Warehouse info, recipient address, tracking number, etc.
55
  """
56
+ # Extract values from row (using .get() with defaults)
57
  warehouse_line1 = row.get("Warehouse Line1", "WAREHOUSE 2")
58
  warehouse_line2 = row.get("Warehouse Line2", "11919 WINK RD")
59
  warehouse_line3 = row.get("Warehouse Line3", "HOUSTON TX 77024-7134")
 
64
  recipient_addr2 = row.get("Recipient Addr2", "Salt Lake City, UT 11212")
65
  recipient_addr3 = row.get("Recipient Addr3", "United States")
66
 
67
+ tracking_number = row.get("Tracking Number", "9400110200793961893691")
68
 
69
  # Warehouse info (top-left under the bar)
70
  c.setFont("Helvetica-Bold", 9)
 
74
  c.drawString(15, height - 160, warehouse_line3)
75
 
76
  # Order number (top-right)
77
+ c.setFont("Helvetica", 9)
78
  c.drawString(width - 80, height - 130, f"Order: {order_number}")
79
 
80
  # Recipient info (bold name)
 
101
  Creates a 4x6 inch PDF label using the collage background,
102
  then overlays dynamic text and a barcode at the bottom.
103
  """
104
+ # Generate Code128 barcode
105
  tracking_number = row.get("Tracking Number", "9400110200793961893691")
106
  barcode_filename = os.path.join(BARCODES_DIR, f"barcode_{index}.png")
107
  code128 = barcode.get_barcode_class("code128")
108
  code128_obj = code128(tracking_number, writer=ImageWriter())
109
  code128_obj.save(barcode_filename)
110
 
111
+ # Prepare a 4x6 inch PDF
112
  width, height = 4 * inch, 6 * inch
113
  label_filename = os.path.join(LABELS_DIR, f"label_{index}.pdf")
114
  c = canvas.Canvas(label_filename, pagesize=(width, height))
 
141
  for f in os.listdir(BARCODES_DIR):
142
  os.remove(os.path.join(BARCODES_DIR, f))
143
 
144
+ # Read the file from the Gradio file object
145
  if file_obj.name.endswith(".csv"):
146
  df = pd.read_csv(file_obj)
147
  elif file_obj.name.endswith((".xls", ".xlsx")):
148
  df = pd.read_excel(file_obj)
149
  else:
150
+ return "Invalid file format. Please upload a .csv, .xls, or .xlsx file.", None
151
 
152
+ # Basic check for required columns
153
  needed_cols = [
154
  "Warehouse Line1", "Warehouse Line2", "Warehouse Line3", "Order Number",
155
  "Recipient Name", "Recipient Addr1", "Recipient Addr2", "Recipient Addr3",
 
157
  ]
158
  missing = [c for c in needed_cols if c not in df.columns]
159
  if missing:
160
+ return f"Missing columns: {', '.join(missing)}", None
161
 
162
  # Generate a PDF label for each row
163
  for i, row in df.iterrows():
164
  create_usps_label(row.to_dict(), index=i+1)
165
 
166
+ # Zip all PDFs into one file
167
  zip_filename = "usps_labels.zip"
168
  with zipfile.ZipFile(zip_filename, "w") as zf:
169
  for f in os.listdir(LABELS_DIR):
170
  if f.endswith(".pdf"):
171
  zf.write(os.path.join(LABELS_DIR, f), f)
172
 
173
+ return "Labels generated successfully.", zip_filename
174
 
175
  except Exception as e:
176
+ return f"Error processing file: {str(e)}", None
177
 
178
  ############################################
179
  # 5) Gradio Interface
 
181
  iface = gr.Interface(
182
  fn=process_file,
183
  inputs=gr.File(type="filepath"),
184
+ outputs=[gr.Text(label="Status"), gr.File(label="Download Zipped Labels")],
185
  title="USPS-Style Label Generator (4x6)",
186
  description=(
187
  "Upload a CSV/XLS/XLSX with columns:\n"