Last active
February 18, 2024 22:40
-
-
Save marrold/d0f2a8e2c0d992e7afccf0c358caefc9 to your computer and use it in GitHub Desktop.
Converts the emf schedule to a PDF for faxing
This file contains 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
# API documentation - https://developer.emfcamp.org/schedule | |
from fpdf import FPDF | |
# Sample data | |
data = { | |
"stage-a": [ | |
{ | |
"title": "Exploring Quantum Computing", | |
"speaker": "Alice Johnson", | |
"start_time": "09:00", | |
"end_time": "10:00", | |
"description": "An introduction to quantum computing principles and the future of technology." | |
}, | |
{ | |
"title": "The Future of AI", | |
"speaker": "Bob Smith", | |
"start_time": "10:30", | |
"end_time": "11:30", | |
"description": "Discussing the advancements and ethical considerations in artificial intelligence." | |
} | |
], | |
"stage-b": [ | |
{ | |
"title": "Virtual Reality in 2024", | |
"speaker": "Clara Davis", | |
"start_time": "09:15", | |
"end_time": "10:15", | |
"description": "Exploring the current state and future possibilities of virtual reality." | |
}, | |
{ | |
"title": "Blockchain and Society", | |
"speaker": "Daniel Lee", | |
"start_time": "10:45", | |
"end_time": "11:45", | |
"description": "Understanding how blockchain technology is impacting various sectors of society." | |
} | |
], | |
"stage-c": [ | |
{ | |
"title": "Cybersecurity Trends", | |
"speaker": "Eva Grant", | |
"start_time": "09:30", | |
"end_time": "10:30", | |
"description": "A look at the latest trends in cybersecurity and how to protect against emerging threats." | |
}, | |
{ | |
"title": "Renewable Energy Sources", | |
"speaker": "Frank Harris", | |
"start_time": "11:00", | |
"end_time": "12:00", | |
"description": "An overview of renewable energy sources and their impact on the environment." | |
} | |
] | |
} | |
class PDF(FPDF): | |
def header(self): | |
self.set_font('Arial', 'B', 16) | |
title = 'EMF - Now & Next' if self.page_no() == 1 else 'EMF - Now & Next (Continued)' | |
self.cell(0, 10, title, 0, 1, 'C') | |
def check_page_break(self, event_height): | |
if self.get_y() + event_height > 260: | |
self.add_page() | |
def add_event(self, event): | |
# Calculate the effective page width | |
effective_page_width = self.w - self.l_margin - self.r_margin | |
# Estimate the height required for the event | |
event_height = 10 + 10 + self.get_string_width(event["description"]) / effective_page_width * 10 + 10 | |
self.check_page_break(event_height) | |
self.set_fill_color(220, 220, 220) # Grey background for the title | |
self.set_font('Arial', 'B', 12) # Bold font for the title | |
self.cell(0, 10, f"Title: {event['title']}", 0, 1, fill=True) | |
self.set_font('Arial', '', 12) # Regular font for other details | |
self.cell(100, 10, f"Speaker: {event['speaker']}") | |
self.cell(100, 10, f"Time: {event['start_time']} - {event['end_time']}", 0, 1) | |
self.multi_cell(0, 10, event["description"]) | |
self.ln(10) | |
pdf = PDF() | |
pdf.add_page() | |
for stage, events in data.items(): | |
stage_name = stage.replace('stage-', '').capitalize() # Remove 'stage-' prefix and capitalize | |
pdf.set_font('Arial', 'B', 14) | |
pdf.cell(0, 10, f"Stage {stage_name}", 0, 1, 'L') | |
for event in events: | |
pdf.add_event(event) | |
pdf.output("Event_Schedule.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment