Created
September 23, 2024 03:21
-
-
Save tirzasrwn/9850fd02d50a1a9a936082abb24e5222 to your computer and use it in GitHub Desktop.
Example of Certificate Generator By Name List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from pdfrw import PdfReader, PdfWriter, PageMerge | |
from reportlab.pdfgen import canvas | |
# Load the names from the CSV file | |
names_df = pd.read_csv('names.csv') | |
# Path to your PDF template | |
template_path = 'template.pdf' | |
# Read the template PDF to get its size | |
template_pdf = PdfReader(template_path) | |
template_page = template_pdf.pages[0] | |
template_width = float(template_page.MediaBox[2]) # Width of the page | |
template_height = float(template_page.MediaBox[3]) # Height of the page | |
# Function to create a temporary PDF with the name | |
def create_temp_pdf(name, temp_pdf_path): | |
c = canvas.Canvas(temp_pdf_path, pagesize=(template_width, template_height)) # Use custom size | |
# Set the font type and size | |
c.setFont("Helvetica-Bold", 16) | |
c.drawString(100, template_height - 100, 'Certificate of Achievement') | |
c.setFont("Helvetica", 12) | |
c.drawString(100, template_height - 150, 'This certifies that') | |
c.drawString(100, template_height - 200, name) | |
c.drawString(100, template_height - 250, 'has completed the course.') | |
c.save() | |
# Generate PDFs for each name | |
for index, row in names_df.iterrows(): | |
# Create a temporary PDF with the name | |
temp_pdf_path = f'temp_{index}.pdf' | |
create_temp_pdf(row['name'], temp_pdf_path) | |
# Merge the temporary PDF with the template | |
for page in template_pdf.pages: | |
# Merge the temp PDF page onto the template page | |
temp_pdf = PdfReader(temp_pdf_path) | |
PageMerge(page).add(temp_pdf.pages[0]).render() | |
# Save the new PDF with the name | |
pdf_file_name = f"{row['name'].replace(' ', '_')}_certificate.pdf" | |
PdfWriter(pdf_file_name, trailer=template_pdf).write() | |
print("PDFs generated successfully.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment