File size: 5,082 Bytes
5f132fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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):
    # قراءة البيانات من ملف Excel
    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} غير موجود في الملف!"

    # تنظيف النصوص
    def clean_text(text):
        if pd.isna(text):
            return "غير متوفر"
        return str(text).strip()

    data["Description"] = data["Description"].apply(clean_text)
    data["Supplier Name"] = data["Supplier Name"].apply(clean_text)

    # إعداد مجلدات الإخراج
    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)

    # إعداد الخط
    try:
        font = ImageFont.truetype(font_path, 14)
    except OSError:
        font = ImageFont.load_default()

    # إعداد أبعاد الملصق
    label_width = 400
    label_height = 300
    qr_size = 155

    # قائمة لتخزين بيانات QR Code
    qr_data_list = []

    # إنشاء QR Codes وملصقات
    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
        qr_info = f"Asset-Code: {asset_code}, Location: {location}"
        
        # إنشاء 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)

        # حفظ QR Code كصورة
        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)

        # إنشاء الملصق
        label = Image.new("RGB", (label_width, label_height), "white")
        draw = ImageDraw.Draw(label)

        # لصق QR Code
        qr_x = 20
        qr_y = (label_height - qr_size) // 2
        label.paste(qr_image.resize((qr_size, qr_size)), (qr_x, qr_y))

        # إضافة النصوص بجانب QR Code
        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))

        # إضافة البيانات إلى القائمة
        qr_data_list.append({
            "Asset-Code": asset_code,
            "Location": location,
            "Description": row["Description"],
            "Supplier Name": row["Supplier Name"]
        })

    # ضغط الملفات إلى ملف ZIP
    shutil.make_archive("labels_with_qr", 'zip', output_folder)
    shutil.move("labels_with_qr.zip", zip_output_path)

    # حفظ بيانات QR Code في ملف Excel
    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')

    # حفظ البيانات في ملف CSV
    output_csv = "qr_data_output.csv"
    qr_data_df.to_csv(output_csv, index=False)

    # إرجاع روابط التنزيل
    return zip_output_path, output_excel, output_csv

# واجهة 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="QR code generator",
    description="توليد QR Codes وملصقات بناءً على البيانات المدخلة"
)

if __name__ == "__main__":
    interface.launch(share=True)