|  | 
|  | 1 | +import csv | 
|  | 2 | +import os | 
|  | 3 | +from PyPDF2 import PdfReader, PdfWriter | 
|  | 4 | +from reportlab.lib.pagesizes import letter | 
|  | 5 | +from reportlab.pdfgen import canvas | 
|  | 6 | +from PIL import Image, ImageDraw, ImageFont | 
|  | 7 | + | 
|  | 8 | +def write_names_on_file(input_file, csv_path, output_folder): | 
|  | 9 | +    name, ext = os.path.splitext(input_file) | 
|  | 10 | +    if ext.lower() == '.pdf': | 
|  | 11 | +        write_names_on_pdf(input_file, csv_path, output_folder) | 
|  | 12 | +    elif ext.lower() in ['.jpg', '.jpeg', '.png', '.gif']: | 
|  | 13 | +        write_names_on_image(input_file, csv_path, output_folder) | 
|  | 14 | +    else: | 
|  | 15 | +        print(f"Unsupported file type: {ext}") | 
|  | 16 | + | 
|  | 17 | +def get_middle_coordinates(width, height, text, font_size): | 
|  | 18 | +    text_width = font_size * len(text) * 0.6  # Rough approximation of text width | 
|  | 19 | +    x = (width - text_width) / 2 | 
|  | 20 | +    y = (height - font_size) / 2 | 
|  | 21 | +    return x, y | 
|  | 22 | + | 
|  | 23 | +def write_names_on_pdf(pdf_path, csv_path, output_folder): | 
|  | 24 | +    with open(csv_path, 'r') as csv_file: | 
|  | 25 | +        csv_reader = csv.DictReader(csv_file) | 
|  | 26 | +        for row in csv_reader: | 
|  | 27 | +            name = row['name'] | 
|  | 28 | +            output_pdf_path = os.path.join(output_folder, f"{name}.pdf") | 
|  | 29 | +            reader = PdfReader(pdf_path) | 
|  | 30 | +            writer = PdfWriter() | 
|  | 31 | + | 
|  | 32 | +            for page_num in range(len(reader.pages)): | 
|  | 33 | +                page = reader.pages[page_num] | 
|  | 34 | +                writer.add_page(page) | 
|  | 35 | + | 
|  | 36 | +                width, height = letter | 
|  | 37 | +                x, y = get_middle_coordinates(width, height, name, 12)  # Adjust font size as needed | 
|  | 38 | +                packet = canvas.Canvas("temp.pdf", pagesize=letter) | 
|  | 39 | +                # packet.drawString(x-200, y-40, ' '.join(name.split()[:2]), fontName="Helvetica", fontSize=12) | 
|  | 40 | +                packet.setFont("Helvetica", 12)  # Setting font to Helvetica with size 12 | 
|  | 41 | +                packet.drawString(x-200, y-40, ' '.join(name.split()[:2])) | 
|  | 42 | +                packet.save() | 
|  | 43 | + | 
|  | 44 | +                overlay = PdfReader("temp.pdf") | 
|  | 45 | +                page.merge_page(overlay.pages[0]) | 
|  | 46 | + | 
|  | 47 | +            with open(output_pdf_path, 'wb') as output_pdf: | 
|  | 48 | +                writer.write(output_pdf) | 
|  | 49 | + | 
|  | 50 | +            os.remove("temp.pdf")  # Remove temporary file | 
|  | 51 | + | 
|  | 52 | +def write_names_on_image(image_path, csv_path, output_folder): | 
|  | 53 | +    with open(csv_path, 'r') as csv_file: | 
|  | 54 | +        csv_reader = csv.DictReader(csv_file) | 
|  | 55 | +        for row in csv_reader: | 
|  | 56 | +            name = row['name'] | 
|  | 57 | +            output_image_path = os.path.join(output_folder, f"{name}{os.path.splitext(image_path)[1]}") | 
|  | 58 | +            image = Image.open(image_path) | 
|  | 59 | +            draw = ImageDraw.Draw(image) | 
|  | 60 | +            width, height = image.size | 
|  | 61 | +            x, y = get_middle_coordinates(width, height, ' '.join(name.split()[:2]), 40)  # Adjust font size as needed | 
|  | 62 | +            draw.text((x-200, y-40), name.title(), fill="black", font=ImageFont.truetype("arial.ttf", size=30)) | 
|  | 63 | +            image.save(output_image_path) | 
|  | 64 | + | 
|  | 65 | + | 
|  | 66 | +input_file = "data/template.png" | 
|  | 67 | +csv_file = "data/names.csv" | 
|  | 68 | +output_folder = "certificates" | 
|  | 69 | + | 
|  | 70 | +if not os.path.exists(output_folder): | 
|  | 71 | +    os.makedirs(output_folder) | 
|  | 72 | + | 
|  | 73 | +write_names_on_file(input_file, csv_file, output_folder) | 
0 commit comments