Create App.py
Browse files
App.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from reportlab.lib.pagesizes import inch
|
| 4 |
+
from reportlab.pdfgen import canvas
|
| 5 |
+
from reportlab.lib.utils import ImageReader
|
| 6 |
+
import barcode
|
| 7 |
+
from barcode.writer import ImageWriter
|
| 8 |
+
import os
|
| 9 |
+
import zipfile
|
| 10 |
+
|
| 11 |
+
# Ensure 'labels' directory exists
|
| 12 |
+
os.makedirs("labels", exist_ok=True)
|
| 13 |
+
|
| 14 |
+
def generate_label(data, index):
|
| 15 |
+
"""
|
| 16 |
+
Generate a 4x6 inch shipping label as a PDF.
|
| 17 |
+
"""
|
| 18 |
+
pdf_filename = f"labels/shipping_label_{index+1}.pdf"
|
| 19 |
+
c = canvas.Canvas(pdf_filename, pagesize=(4 * inch, 6 * inch))
|
| 20 |
+
|
| 21 |
+
# Draw sender details (top-left)
|
| 22 |
+
c.setFont("Helvetica-Bold", 12)
|
| 23 |
+
c.drawString(30, 500, "From:")
|
| 24 |
+
c.setFont("Helvetica", 10)
|
| 25 |
+
c.drawString(30, 485, data['Sender Name'])
|
| 26 |
+
c.drawString(30, 470, data['Sender Address'])
|
| 27 |
+
c.drawString(30, 455, data['Sender City, State ZIP'])
|
| 28 |
+
|
| 29 |
+
# Draw recipient details (middle)
|
| 30 |
+
c.setFont("Helvetica-Bold", 12)
|
| 31 |
+
c.drawString(30, 400, "To:")
|
| 32 |
+
c.setFont("Helvetica", 10)
|
| 33 |
+
c.drawString(30, 385, data['Recipient Name'])
|
| 34 |
+
c.drawString(30, 370, data['Recipient Address'])
|
| 35 |
+
c.drawString(30, 355, data['Recipient City, State ZIP'])
|
| 36 |
+
|
| 37 |
+
# Draw shipping service
|
| 38 |
+
c.setFont("Helvetica-Bold", 12)
|
| 39 |
+
c.drawString(30, 320, f"Service: {data['Shipping Service']}")
|
| 40 |
+
|
| 41 |
+
# Draw tracking number
|
| 42 |
+
c.setFont("Helvetica-Bold", 12)
|
| 43 |
+
c.drawString(30, 280, "Tracking #:")
|
| 44 |
+
c.setFont("Helvetica", 12)
|
| 45 |
+
c.drawString(30, 260, data['Tracking Number'])
|
| 46 |
+
|
| 47 |
+
# Generate barcode
|
| 48 |
+
barcode_filename = f"labels/barcode_{index+1}.png"
|
| 49 |
+
ean = barcode.get_barcode_class('code128')
|
| 50 |
+
ean_obj = ean(data['Tracking Number'], writer=ImageWriter())
|
| 51 |
+
ean_obj.save(barcode_filename)
|
| 52 |
+
|
| 53 |
+
# Add barcode to label
|
| 54 |
+
barcode_img = ImageReader(barcode_filename)
|
| 55 |
+
c.drawImage(barcode_img, 30, 120, width=300, height=100)
|
| 56 |
+
|
| 57 |
+
# Save PDF
|
| 58 |
+
c.showPage()
|
| 59 |
+
c.save()
|
| 60 |
+
|
| 61 |
+
def process_csv(file):
|
| 62 |
+
"""
|
| 63 |
+
Read CSV and generate labels.
|
| 64 |
+
"""
|
| 65 |
+
df = pd.read_csv(file.name)
|
| 66 |
+
|
| 67 |
+
# Generate labels for each row
|
| 68 |
+
for i, row in df.iterrows():
|
| 69 |
+
generate_label(row, i)
|
| 70 |
+
|
| 71 |
+
# Zip all PDFs into a single file for download
|
| 72 |
+
zip_filename = "labels/shipping_labels.zip"
|
| 73 |
+
with zipfile.ZipFile(zip_filename, "w") as zipf:
|
| 74 |
+
for label_file in os.listdir("labels"):
|
| 75 |
+
if label_file.endswith(".pdf"):
|
| 76 |
+
zipf.write(f"labels/{label_file}", label_file)
|
| 77 |
+
|
| 78 |
+
return zip_filename
|
| 79 |
+
|
| 80 |
+
# Gradio Interface
|
| 81 |
+
interface = gr.Interface(
|
| 82 |
+
fn=process_csv,
|
| 83 |
+
inputs=gr.File(label="Upload CSV File"),
|
| 84 |
+
outputs=gr.File(label="Download Shipping Labels (ZIP)"),
|
| 85 |
+
title="4x6 Shipping Label Generator",
|
| 86 |
+
description="Upload a CSV file with shipping details, and this app will generate professional 4x6 shipping labels with barcodes.",
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Run the app
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
interface.launch()
|