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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -131
app.py CHANGED
@@ -1,131 +1,149 @@
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}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_files(file1, file2):
18
+ # قراءة البيانات من ملفات Excel
19
+ data1 = pd.read_excel(file1.name, engine="openpyxl").rename(columns=lambda x: x.strip())
20
+ data2 = pd.read_excel(file2.name, engine="openpyxl").rename(columns=lambda x: x.strip())
21
+
22
+ # دمج البيانات
23
+ merged_data = pd.concat([data1, data2], ignore_index=True)
24
+
25
+ # التحقق من وجود الأعمدة المطلوبة
26
+ required_columns = ['Asset-Code', 'Location', 'Unit', 'Description', 'Supplier Name']
27
+ for column in required_columns:
28
+ if column not in merged_data.columns:
29
+ return f"العمود {column} غير موجود في الملفات!"
30
+
31
+ # تنظيف النصوص
32
+ def clean_text(text):
33
+ if pd.isna(text):
34
+ return "غير متوفر"
35
+ return str(text).strip()
36
+
37
+ merged_data["Description"] = merged_data["Description"].apply(clean_text)
38
+ merged_data["Supplier Name"] = merged_data["Supplier Name"].apply(clean_text)
39
+
40
+ # إعداد مجلدات الإخراج
41
+ qr_folder = "qr_codes_with_units"
42
+ output_folder = "labels_output"
43
+ zip_output_path = "Labels_with_QRCodes.zip"
44
+ os.makedirs(qr_folder, exist_ok=True)
45
+ os.makedirs(output_folder, exist_ok=True)
46
+
47
+ # إعداد الخط
48
+ try:
49
+ font = ImageFont.truetype(font_path, 14)
50
+ except OSError:
51
+ font = ImageFont.load_default()
52
+
53
+ # إعداد أبعاد الملصق
54
+ label_width = 400
55
+ label_height = 300
56
+ qr_size = 155
57
+
58
+ # قائمة لتخزين بيانات QR Code
59
+ qr_data_list = []
60
+
61
+ # إنشاء QR Codes وملصقات
62
+ for _, row in merged_data.iterrows():
63
+ asset_code = str(row["Asset-Code"])
64
+ location = clean_text(row["Location"])
65
+ description = reshape(clean_text(row["Description"]))
66
+ supplier_name = reshape(clean_text(row["Supplier Name"]))
67
+
68
+ # معالجة القيم الفارغة في Unit
69
+ unit_count = int(row["Unit"]) if pd.notna(row["Unit"]) else 1
70
+
71
+ for i in range(1, unit_count + 1):
72
+ # إنشاء نص QR
73
+ qr_info = f"Asset-Code: {asset_code}, Location: {location}, Unit: {i}"
74
+
75
+ # إنشاء QR Code
76
+ qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=5, border=1)
77
+ qr.add_data(qr_info)
78
+ qr.make(fit=True)
79
+
80
+ # حفظ QR Code كصورة
81
+ qr_file_name = f"{asset_code}-{i}.png"
82
+ qr_file_path = os.path.join(qr_folder, qr_file_name)
83
+ qr_image = qr.make_image(fill_color="black", back_color="white")
84
+ qr_image.save(qr_file_path)
85
+
86
+ # إنشاء الملصق
87
+ label = Image.new("RGB", (label_width, label_height), "white")
88
+ draw = ImageDraw.Draw(label)
89
+
90
+ # لصق QR Code
91
+ qr_x = 20
92
+ qr_y = (label_height - qr_size) // 2
93
+ label.paste(qr_image.resize((qr_size, qr_size)), (qr_x, qr_y))
94
+
95
+ # إضافة النصوص بجانب QR Code
96
+ text_x = qr_x + qr_size + 15
97
+ text_y = 50
98
+ line_spacing = 40
99
+ draw.text((text_x, text_y), f"اسم الصنف: {description}", font=font, fill="black")
100
+ draw.text((text_x, text_y + line_spacing), f"اسم المورد: {supplier_name}", font=font, fill="black")
101
+ draw.text((text_x, text_y + 2 * line_spacing), f"كود الصنف: {asset_code}", font=font, fill="black")
102
+
103
+ # حفظ الملصق
104
+ label_file_name = f"{asset_code}-{i}_label.png"
105
+ label.save(os.path.join(output_folder, label_file_name))
106
+
107
+ # إضافة البيانات إلى القائمة
108
+ qr_data_list.append({
109
+ "Asset-Code": asset_code,
110
+ "Location": location,
111
+ "Unit": i,
112
+ "Description": row["Description"],
113
+ "Supplier Name": row["Supplier Name"]
114
+ })
115
+
116
+ # ضغط الملفات إلى ملف ZIP
117
+ shutil.make_archive("labels_with_qr", 'zip', output_folder)
118
+ shutil.move("labels_with_qr.zip", zip_output_path)
119
+
120
+ # حفظ بيانات QR Code في ملف Excel
121
+ output_excel = "qr_data_output.xlsx"
122
+ qr_data_df = pd.DataFrame(qr_data_list)
123
+ qr_data_df.to_excel(output_excel, index=False, engine='openpyxl')
124
+
125
+ # حفظ البيانات في ملف CSV
126
+ output_csv = "qr_data_output.csv"
127
+ qr_data_df.to_csv(output_csv, index=False)
128
+
129
+ # إرجاع روابط التنزيل
130
+ return zip_output_path, output_excel, output_csv
131
+
132
+ # واجهة Gradio
133
+ interface = gr.Interface(
134
+ fn=process_files,
135
+ inputs=[
136
+ gr.File(label="اختر ملف Excel الأول"),
137
+ gr.File(label="اختر ملف Excel الثاني")
138
+ ],
139
+ outputs=[
140
+ gr.File(label="تحميل ملف ZIP الناتج"),
141
+ gr.File(label="تحميل ملف Excel الناتج"),
142
+ gr.File(label="تحميل ملف CSV الناتج")
143
+ ],
144
+ title="مولد أكواد QR",
145
+ description="قم برفع ملفي Excel لتوليد أكواد QR وحفظ النتائج في ملفات قابلة للتحميل."
146
+ )
147
+
148
+ if __name__ == "__main__":
149
+ interface.launch()