Spaces:
Sleeping
Sleeping
Update app.py
Browse files14 unmodified lines151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021 unmodified line10410510610710810911011111211311411511611711811912012112212312412512614 unmodified linesf.write(response.content)def process_file(file1):# قراءة البيانات من ملف Exceldata = 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 = 400label_height = 300qr_size = 155# قائمة لتخزين بيانات QR Codeqr_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"]))# إنشاء نص QRqr_info = f"Asset-Code: {asset_code}, Location: {location}"# إنشاء QR Codeqr = 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 Codeqr_x = 20qr_y = (label_height - qr_size) // 2label.paste(qr_image.resize((qr_size, qr_size)), (qr_x, qr_y))# إضافة النصوص بجانب QR Codetext_x = qr_x + qr_size + 15text_y = 50line_spacing = 40draw.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,1 unmodified line"Supplier Name": row["Supplier Name"]})# ضغط الملفات إلى ملف ZIPshutil.make_archive("labels_with_qr", 'zip', output_folder)shutil.move("labels_with_qr.zip", zip_output_path)# حفظ بيانات QR Code في ملف Exceloutput_excel = "qr_data_output.xlsx"qr_data_df = pd.DataFrame(qr_data_list)qr_data_df.to_excel(output_excel, index=False, engine='openpyxl')# حفظ البيانات في ملف CSVoutput_csv = "qr_data_output.csv"qr_data_df.to_csv(output_csv, index=False)# إرجاع روابط التنزيلreturn zip_output_path, output_excel, output_csv# واجهة Gradiointerface = gr.Interface(fn=process_file,inputs=[14 unmodified lines1516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091921 unmodified line94959697989910010110210310410510610710810911011111214 unmodified linesf.write(response.content)def process_file(file1):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 {column}Not found!"def clean_text(text):if pd.isna(text):return "Null"return str(text).strip()data["Description"] = data["Description"].apply(clean_text)data["Supplier Name"] = data["Supplier Name"].apply(clean_text)# output filesqr_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)# Font settingtry:font = ImageFont.truetype(font_path, 14)except OSError:font = ImageFont.load_default()# label designlabel_width = 400label_height = 300qr_size = 155#saving dataqr_data_list = []# Generating QRcodefor _, 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 code textqr_info = f"Asset-Code: {asset_code}, Location: {location}"# generate QR Codeqr = 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)# save QR Code as an imageqr_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)# labelslabel = Image.new("RGB", (label_width, label_height), "white")draw = ImageDraw.Draw(label)qr_x = 20qr_y = (label_height - qr_size) // 2label.paste(qr_image.resize((qr_size, qr_size)), (qr_x, qr_y))text_x = qr_x + qr_size + 15text_y = 50line_spacing = 40draw.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))# adding data to listqr_data_list.append({"Asset-Code": asset_code,"Location": location,1 unmodified line"Supplier Name": row["Supplier Name"]})# preparing outputshutil.make_archive("labels_with_qr", 'zip', output_folder)shutil.move("labels_with_qr.zip", zip_output_path)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')output_csv = "qr_data_output.csv"qr_data_df.to_csv(output_csv, index=False)# إرجاع روابط التنزيلreturn zip_output_path, output_excel, output_csv# User Interface Gradiointerface = gr.Interface(fn=process_file,inputs=[
app.py+14-28