Spaces:
Sleeping
Sleeping
File size: 4,366 Bytes
c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 f5b4ffb c427fb6 36961c8 23aed91 c427fb6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | import pandas as pd
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import requests
import qrcode
from arabic_reshaper import reshape
import gradio as gr
# تحميل خط من الإنترنت
font_url = "https://github.com/google/fonts/blob/main/ofl/amiri/Amiri-Regular.ttf?raw=true"
font_path = "Amiri-Regular.ttf"
response = requests.get(font_url)
with open(font_path, 'wb') as f:
f.write(response.content)
def process_file(file1):
data = pd.read_excel(file1.name, engine="openpyxl").rename(columns=lambda x: x.strip())
required_columns = ['Asset-Code', 'Location', 'Description', 'Supplier Name']
for column in required_columns:
if column not in data.columns:
return f"column {column}Not found!"
def clean_text(text):
if pd.isna(text):
return "Null"
return str(text).strip()
data["Description"] = data["Description"].apply(clean_text)
data["Supplier Name"] = data["Supplier Name"].apply(clean_text)
# output files
qr_folder = "qr_codes"
output_folder = "labels_output"
zip_output_path = "Labels_with_QRCodes.zip"
os.makedirs(qr_folder, exist_ok=True)
os.makedirs(output_folder, exist_ok=True)
# Font setting
try:
font = ImageFont.truetype(font_path, 14)
except OSError:
font = ImageFont.load_default()
# label design
label_width = 400
label_height = 300
qr_size = 155
#saving data
qr_data_list = []
# Generating QRcode
for _, row in data.iterrows():
asset_code = str(row["Asset-Code"])
location = clean_text(row["Location"])
description = reshape(clean_text(row["Description"]))
supplier_name = reshape(clean_text(row["Supplier Name"]))
# QR code text
qr_info = f"Asset-Code: {asset_code}, Location: {location}"
# generate QR Code
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=5, border=1)
qr.add_data(qr_info)
qr.make(fit=True)
# save QR Code as an image
qr_file_name = f"{asset_code}.png"
qr_file_path = os.path.join(qr_folder, qr_file_name)
qr_image = qr.make_image(fill_color="black", back_color="white")
qr_image.save(qr_file_path)
# labels
label = Image.new("RGB", (label_width, label_height), "white")
draw = ImageDraw.Draw(label)
qr_x = 20
qr_y = (label_height - qr_size) // 2
label.paste(qr_image.resize((qr_size, qr_size)), (qr_x, qr_y))
text_x = qr_x + qr_size + 15
text_y = 50
line_spacing = 40
draw.text((text_x, text_y), f"اسم الصنف: {description}", font=font, fill="black")
draw.text((text_x, text_y + line_spacing), f"اسم المورد: {supplier_name}", font=font, fill="black")
draw.text((text_x, text_y + 2 * line_spacing), f"كود الصنف: {asset_code}", font=font, fill="black")
label_file_name = f"{asset_code}_label.png"
label.save(os.path.join(output_folder, label_file_name))
# adding data to list
qr_data_list.append({
"Asset-Code": asset_code,
"Location": location,
"Description": row["Description"],
"Supplier Name": row["Supplier Name"]
})
# preparing output
shutil.make_archive("labels_with_qr", 'zip', output_folder)
shutil.move("labels_with_qr.zip", zip_output_path)
output_excel = "qr_data_output.xlsx"
qr_data_df = pd.DataFrame(qr_data_list)
qr_data_df.to_excel(output_excel, index=False, engine='openpyxl')
output_csv = "qr_data_output.csv"
qr_data_df.to_csv(output_csv, index=False)
# إرجاع روابط التنزيل
return zip_output_path, output_excel, output_csv
# User Interface Gradio
interface = gr.Interface(
fn=process_file,
inputs=[
gr.File(label="اختر ملف Excel")
],
outputs=[
gr.File(label="تحميل ملف ZIP الناتج"),
gr.File(label="تحميل ملف Excel الناتج"),
gr.File(label="تحميل ملف CSV الناتج")
],
title="QRCodeGeneratorAlsalam",
#description="توليد QR Codes وملصقات بناءً على البيانات المدخلة"
)
if __name__ == "__main__":
interface.launch(share=True)
|