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