timfocus commited on
Commit
e390899
·
verified ·
1 Parent(s): e508c3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import os
2
  import pandas as pd
3
  import gradio as gr
4
- from reportlab.lib.pagesizes import landscape, letter
5
  from reportlab.pdfgen import canvas
 
6
  import barcode
7
  from barcode.writer import ImageWriter
8
 
@@ -13,13 +13,13 @@ os.makedirs(LABELS_DIR, exist_ok=True)
13
  os.makedirs(BARCODES_DIR, exist_ok=True)
14
 
15
  # Function to process the uploaded file
16
- def process_file(file):
17
  try:
18
  # Load data from file
19
- if file.name.endswith('.csv'):
20
- df = pd.read_csv(file.name)
21
- elif file.name.endswith(('.xls', '.xlsx')):
22
- df = pd.read_excel(file.name)
23
  else:
24
  return "Invalid file format. Please upload a .csv, .xls, or .xlsx file."
25
 
@@ -33,9 +33,10 @@ def process_file(file):
33
  label_files = []
34
  for i, row in df.iterrows():
35
  label_path = create_shipping_label(row, i + 1)
36
- label_files.append(label_path)
 
37
 
38
- return label_files
39
 
40
  except Exception as e:
41
  return f"Error processing file: {str(e)}"
@@ -53,19 +54,19 @@ def create_shipping_label(row, index):
53
 
54
  # Create PDF label
55
  label_path = os.path.join(LABELS_DIR, f"label_{index}.pdf")
56
- c = canvas.Canvas(label_path, pagesize=(4 * 72, 6 * 72)) # 4x6 inches
57
 
58
  # Add sender & receiver details
59
- c.setFont("Helvetica", 10)
60
- c.drawString(20, 380, f"From: {row['Sender Name']}")
61
- c.drawString(20, 360, f"To: {row['Receiver Name']}")
62
- c.drawString(20, 340, f"Address: {row['Address']}, {row['City']}, {row['State']} {row['Zip']}")
63
 
64
  # Add barcode
65
  if os.path.exists(barcode_path):
66
- c.drawImage(barcode_path, 20, 200, width=200, height=50)
67
  else:
68
- c.drawString(20, 200, "Barcode generation failed.")
69
 
70
  c.showPage()
71
  c.save()
@@ -77,8 +78,8 @@ def create_shipping_label(row, index):
77
  # Gradio interface
78
  iface = gr.Interface(
79
  fn=process_file,
80
- inputs=gr.File(type="file"),
81
- outputs=gr.File(type="file", label="Download Labels"),
82
  title="Shipping Label Generator",
83
  description="Upload a .csv, .xls, or .xlsx file to generate shipping labels."
84
  )
 
1
  import os
2
  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
 
 
13
  os.makedirs(BARCODES_DIR, exist_ok=True)
14
 
15
  # Function to process the uploaded file
16
+ def process_file(file_path):
17
  try:
18
  # Load data from file
19
+ if file_path.endswith('.csv'):
20
+ df = pd.read_csv(file_path)
21
+ elif file_path.endswith(('.xls', '.xlsx')):
22
+ df = pd.read_excel(file_path)
23
  else:
24
  return "Invalid file format. Please upload a .csv, .xls, or .xlsx file."
25
 
 
33
  label_files = []
34
  for i, row in df.iterrows():
35
  label_path = create_shipping_label(row, i + 1)
36
+ if os.path.exists(label_path):
37
+ label_files.append(label_path)
38
 
39
+ return label_files if label_files else "No labels generated."
40
 
41
  except Exception as e:
42
  return f"Error processing file: {str(e)}"
 
54
 
55
  # Create PDF label
56
  label_path = os.path.join(LABELS_DIR, f"label_{index}.pdf")
57
+ c = canvas.Canvas(label_path, pagesize=letter)
58
 
59
  # Add sender & receiver details
60
+ c.setFont("Helvetica", 12)
61
+ c.drawString(50, 750, f"From: {row['Sender Name']}")
62
+ c.drawString(50, 730, f"To: {row['Receiver Name']}")
63
+ c.drawString(50, 710, f"Address: {row['Address']}, {row['City']}, {row['State']} {row['Zip']}")
64
 
65
  # Add barcode
66
  if os.path.exists(barcode_path):
67
+ c.drawImage(barcode_path, 50, 600, width=200, height=50)
68
  else:
69
+ c.drawString(50, 600, "Barcode generation failed.")
70
 
71
  c.showPage()
72
  c.save()
 
78
  # Gradio interface
79
  iface = gr.Interface(
80
  fn=process_file,
81
+ inputs=gr.File(type="filepath"), # FIXED: Use 'filepath' instead of 'file'
82
+ outputs=gr.File(type="filepath", label="Download Labels"),
83
  title="Shipping Label Generator",
84
  description="Upload a .csv, .xls, or .xlsx file to generate shipping labels."
85
  )