Hadeeratef91 commited on
Commit
7648a71
·
verified ·
1 Parent(s): a253150

Upload 2 files

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