Spaces:
Running
Running
import gradio as gr | |
from PIL import Image, ImageDraw, ImageFont | |
import pandas as pd | |
def generate_certificates(Excel_file): | |
output_paths = [] | |
df = pd.read_excel(Excel_file.name) | |
required_columns = ['name', 'ID', 'date', 'event'] | |
if not all(col in df.columns for col in required_columns): | |
return "Error: Excel file must contain 'name', 'ID', 'date', and 'event' columns." | |
df['date'] = pd.to_datetime(df['date'], errors='coerce') | |
df['formatted_date'] = df['date'].dt.strftime('%b %d, %Y') | |
for _, row in df.iterrows(): | |
name = row['name'] | |
id_num = row['ID'] | |
formatted_date = row['formatted_date'] | |
event_s = row['event'] | |
template = Image.open("CertificateOfAttendance.png") | |
draw = ImageDraw.Draw(template) | |
font = ImageFont.truetype('LiberationMono-Regular.ttf', 70) | |
font1 = ImageFont.truetype('LiberationMono-Regular.ttf', 35) | |
font2 = ImageFont.truetype('LiberationMono-Regular.ttf', 40) | |
name_position = (700, 800) | |
id_position = (670, 910) | |
date_position = (850, 1200) | |
event_position = (565, 1041) | |
draw.text(name_position, name, font=font, fill="black") | |
draw.text(id_position, str(id_num), font=font2, fill="black") | |
draw.text(date_position, formatted_date, font=font2, fill="black") | |
draw.text(event_position, event_s, font=font1, fill="black") | |
output_path = f"generated_certificate_{name}.png" | |
template.save(output_path) | |
output_paths.append(output_path) | |
return output_paths | |
Excel_gradio = gr.Interface( | |
fn=generate_certificates, | |
inputs=gr.File(label="Upload Excel File"), | |
title="QuickCerts", | |
description="Upload an Excel File with ('name', 'ID', 'date', and 'event') Columns to Generate Certificates", | |
outputs="gallery" | |
) | |
Excel_gradio.launch() |