Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,88 +15,78 @@ 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"
|
| 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 |
-
#
|
| 55 |
qr_data_list = []
|
| 56 |
|
| 57 |
-
#
|
| 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 |
-
#
|
| 65 |
qr_info = f"Asset-Code: {asset_code}, Location: {location}"
|
| 66 |
|
| 67 |
-
#
|
| 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 |
-
#
|
| 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,
|
|
@@ -104,23 +94,19 @@ def process_file(file1):
|
|
| 104 |
"Supplier Name": row["Supplier Name"]
|
| 105 |
})
|
| 106 |
|
| 107 |
-
#
|
| 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 |
-
#
|
| 124 |
interface = gr.Interface(
|
| 125 |
fn=process_file,
|
| 126 |
inputs=[
|
|
|
|
| 15 |
f.write(response.content)
|
| 16 |
|
| 17 |
def process_file(file1):
|
|
|
|
| 18 |
data = pd.read_excel(file1.name, engine="openpyxl").rename(columns=lambda x: x.strip())
|
|
|
|
|
|
|
| 19 |
required_columns = ['Asset-Code', 'Location', 'Description', 'Supplier Name']
|
| 20 |
for column in required_columns:
|
| 21 |
if column not in data.columns:
|
| 22 |
+
return f"column {column}Not found!"
|
| 23 |
|
|
|
|
| 24 |
def clean_text(text):
|
| 25 |
if pd.isna(text):
|
| 26 |
+
return "Null"
|
| 27 |
return str(text).strip()
|
| 28 |
|
| 29 |
data["Description"] = data["Description"].apply(clean_text)
|
| 30 |
data["Supplier Name"] = data["Supplier Name"].apply(clean_text)
|
| 31 |
|
| 32 |
+
# output files
|
| 33 |
qr_folder = "qr_codes"
|
| 34 |
output_folder = "labels_output"
|
| 35 |
zip_output_path = "Labels_with_QRCodes.zip"
|
| 36 |
os.makedirs(qr_folder, exist_ok=True)
|
| 37 |
os.makedirs(output_folder, exist_ok=True)
|
| 38 |
|
| 39 |
+
# Font setting
|
| 40 |
try:
|
| 41 |
font = ImageFont.truetype(font_path, 14)
|
| 42 |
except OSError:
|
| 43 |
font = ImageFont.load_default()
|
| 44 |
|
| 45 |
+
# label design
|
| 46 |
label_width = 400
|
| 47 |
label_height = 300
|
| 48 |
qr_size = 155
|
| 49 |
|
| 50 |
+
#saving data
|
| 51 |
qr_data_list = []
|
| 52 |
|
| 53 |
+
# Generating QRcode
|
| 54 |
for _, row in data.iterrows():
|
| 55 |
asset_code = str(row["Asset-Code"])
|
| 56 |
location = clean_text(row["Location"])
|
| 57 |
description = reshape(clean_text(row["Description"]))
|
| 58 |
supplier_name = reshape(clean_text(row["Supplier Name"]))
|
| 59 |
|
| 60 |
+
# QR code text
|
| 61 |
qr_info = f"Asset-Code: {asset_code}, Location: {location}"
|
| 62 |
|
| 63 |
+
# generate QR Code
|
| 64 |
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=5, border=1)
|
| 65 |
qr.add_data(qr_info)
|
| 66 |
qr.make(fit=True)
|
| 67 |
|
| 68 |
+
# save QR Code as an image
|
| 69 |
qr_file_name = f"{asset_code}.png"
|
| 70 |
qr_file_path = os.path.join(qr_folder, qr_file_name)
|
| 71 |
qr_image = qr.make_image(fill_color="black", back_color="white")
|
| 72 |
qr_image.save(qr_file_path)
|
| 73 |
|
| 74 |
+
# labels
|
| 75 |
label = Image.new("RGB", (label_width, label_height), "white")
|
| 76 |
draw = ImageDraw.Draw(label)
|
|
|
|
|
|
|
| 77 |
qr_x = 20
|
| 78 |
qr_y = (label_height - qr_size) // 2
|
| 79 |
label.paste(qr_image.resize((qr_size, qr_size)), (qr_x, qr_y))
|
|
|
|
|
|
|
| 80 |
text_x = qr_x + qr_size + 15
|
| 81 |
text_y = 50
|
| 82 |
line_spacing = 40
|
| 83 |
draw.text((text_x, text_y), f"اسم الصنف: {description}", font=font, fill="black")
|
| 84 |
draw.text((text_x, text_y + line_spacing), f"اسم المورد: {supplier_name}", font=font, fill="black")
|
| 85 |
draw.text((text_x, text_y + 2 * line_spacing), f"كود الصنف: {asset_code}", font=font, fill="black")
|
|
|
|
|
|
|
| 86 |
label_file_name = f"{asset_code}_label.png"
|
| 87 |
label.save(os.path.join(output_folder, label_file_name))
|
| 88 |
|
| 89 |
+
# adding data to list
|
| 90 |
qr_data_list.append({
|
| 91 |
"Asset-Code": asset_code,
|
| 92 |
"Location": location,
|
|
|
|
| 94 |
"Supplier Name": row["Supplier Name"]
|
| 95 |
})
|
| 96 |
|
| 97 |
+
# preparing output
|
| 98 |
shutil.make_archive("labels_with_qr", 'zip', output_folder)
|
| 99 |
shutil.move("labels_with_qr.zip", zip_output_path)
|
|
|
|
|
|
|
| 100 |
output_excel = "qr_data_output.xlsx"
|
| 101 |
qr_data_df = pd.DataFrame(qr_data_list)
|
| 102 |
qr_data_df.to_excel(output_excel, index=False, engine='openpyxl')
|
|
|
|
|
|
|
| 103 |
output_csv = "qr_data_output.csv"
|
| 104 |
qr_data_df.to_csv(output_csv, index=False)
|
| 105 |
|
| 106 |
# إرجاع روابط التنزيل
|
| 107 |
return zip_output_path, output_excel, output_csv
|
| 108 |
|
| 109 |
+
# User Interface Gradio
|
| 110 |
interface = gr.Interface(
|
| 111 |
fn=process_file,
|
| 112 |
inputs=[
|