Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 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)
|
|
@@ -18,7 +18,7 @@ def generate_label(data, index):
|
|
| 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
|
| 22 |
c.setFont("Helvetica-Bold", 12)
|
| 23 |
c.drawString(30, 500, "From:")
|
| 24 |
c.setFont("Helvetica", 10)
|
|
@@ -26,7 +26,7 @@ def generate_label(data, index):
|
|
| 26 |
c.drawString(30, 470, data['Sender Address'])
|
| 27 |
c.drawString(30, 455, data['Sender City, State ZIP'])
|
| 28 |
|
| 29 |
-
# Draw recipient details
|
| 30 |
c.setFont("Helvetica-Bold", 12)
|
| 31 |
c.drawString(30, 400, "To:")
|
| 32 |
c.setFont("Helvetica", 10)
|
|
@@ -58,30 +58,35 @@ def generate_label(data, index):
|
|
| 58 |
c.showPage()
|
| 59 |
c.save()
|
| 60 |
|
| 61 |
-
def process_file(
|
| 62 |
"""
|
| 63 |
-
Read
|
| 64 |
"""
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# Gradio Interface
|
| 87 |
interface = gr.Interface(
|
|
@@ -94,4 +99,4 @@ interface = gr.Interface(
|
|
| 94 |
|
| 95 |
# Run the app
|
| 96 |
if __name__ == "__main__":
|
| 97 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import zipfile
|
| 5 |
from reportlab.lib.pagesizes import inch
|
| 6 |
from reportlab.pdfgen import canvas
|
| 7 |
from reportlab.lib.utils import ImageReader
|
| 8 |
import barcode
|
| 9 |
from barcode.writer import ImageWriter
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Ensure 'labels' directory exists
|
| 12 |
os.makedirs("labels", exist_ok=True)
|
|
|
|
| 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
|
| 22 |
c.setFont("Helvetica-Bold", 12)
|
| 23 |
c.drawString(30, 500, "From:")
|
| 24 |
c.setFont("Helvetica", 10)
|
|
|
|
| 26 |
c.drawString(30, 470, data['Sender Address'])
|
| 27 |
c.drawString(30, 455, data['Sender City, State ZIP'])
|
| 28 |
|
| 29 |
+
# Draw recipient details
|
| 30 |
c.setFont("Helvetica-Bold", 12)
|
| 31 |
c.drawString(30, 400, "To:")
|
| 32 |
c.setFont("Helvetica", 10)
|
|
|
|
| 58 |
c.showPage()
|
| 59 |
c.save()
|
| 60 |
|
| 61 |
+
def process_file(file_obj):
|
| 62 |
"""
|
| 63 |
+
Read CSV or Excel file and generate shipping labels.
|
| 64 |
"""
|
| 65 |
+
try:
|
| 66 |
+
# Read file into Pandas
|
| 67 |
+
file_path = file_obj.name
|
| 68 |
+
if file_path.endswith('.csv'):
|
| 69 |
+
df = pd.read_csv(file_path)
|
| 70 |
+
elif file_path.endswith(('.xls', '.xlsx')):
|
| 71 |
+
df = pd.read_excel(file_path)
|
| 72 |
+
else:
|
| 73 |
+
return "Invalid file format. Please upload a .csv, .xls, or .xlsx file."
|
| 74 |
+
|
| 75 |
+
# Generate labels for each row
|
| 76 |
+
for i, row in df.iterrows():
|
| 77 |
+
generate_label(row, i)
|
| 78 |
+
|
| 79 |
+
# Zip all PDFs into a single file for download
|
| 80 |
+
zip_filename = "labels/shipping_labels.zip"
|
| 81 |
+
with zipfile.ZipFile(zip_filename, "w") as zipf:
|
| 82 |
+
for label_file in os.listdir("labels"):
|
| 83 |
+
if label_file.endswith(".pdf"):
|
| 84 |
+
zipf.write(f"labels/{label_file}", label_file)
|
| 85 |
+
|
| 86 |
+
return zip_filename
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"Error processing file: {str(e)}"
|
| 90 |
|
| 91 |
# Gradio Interface
|
| 92 |
interface = gr.Interface(
|
|
|
|
| 99 |
|
| 100 |
# Run the app
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
interface.launch(share=True)
|