Update app.py
Browse files
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(
|
| 17 |
try:
|
| 18 |
# Load data from file
|
| 19 |
-
if
|
| 20 |
-
df = pd.read_csv(
|
| 21 |
-
elif
|
| 22 |
-
df = pd.read_excel(
|
| 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 |
-
|
|
|
|
| 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=
|
| 57 |
|
| 58 |
# Add sender & receiver details
|
| 59 |
-
c.setFont("Helvetica",
|
| 60 |
-
c.drawString(
|
| 61 |
-
c.drawString(
|
| 62 |
-
c.drawString(
|
| 63 |
|
| 64 |
# Add barcode
|
| 65 |
if os.path.exists(barcode_path):
|
| 66 |
-
c.drawImage(barcode_path,
|
| 67 |
else:
|
| 68 |
-
c.drawString(
|
| 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="
|
| 81 |
-
outputs=gr.File(type="
|
| 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 |
)
|