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): 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 files 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) # Font setting try: font = ImageFont.truetype(font_path, 14) except OSError: font = ImageFont.load_default() # label design label_width = 400 label_height = 300 qr_size = 155 #saving data qr_data_list = [] # Generating QRcode 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 code text qr_info = f"Asset-Code: {asset_code}, Location: {location}" # generate 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) # save QR Code as an image 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) # labels label = Image.new("RGB", (label_width, label_height), "white") draw = ImageDraw.Draw(label) qr_x = 20 qr_y = (label_height - qr_size) // 2 label.paste(qr_image.resize((qr_size, qr_size)), (qr_x, qr_y)) 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)) # adding data to list qr_data_list.append({ "Asset-Code": asset_code, "Location": location, "Description": row["Description"], "Supplier Name": row["Supplier Name"] }) # preparing output shutil.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 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="QRCodeGeneratorAlsalam", #description="توليد QR Codes وملصقات بناءً على البيانات المدخلة" ) if __name__ == "__main__": interface.launch(share=True)