Skip to content

Instantly share code, notes, and snippets.

@adgedenkers
Created February 1, 2025 16:12
Show Gist options
  • Save adgedenkers/fb2bbebfc95a706fc63685b57da7a05e to your computer and use it in GitHub Desktop.
Save adgedenkers/fb2bbebfc95a706fc63685b57da7a05e to your computer and use it in GitHub Desktop.
# file: bingo_card_generator.py
# ---------------------------------------------------------
# author: Adge Denkers
# github: @adgedenkers
# created: 2025-02-01
# updated: 2025-02-01
# version: 1.0.0
import numpy as np
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
from openpyxl.formatting.rule import CellIsRule
def generate_bingo_cards(terms, num_cards=6, output_file="SuperBowl_Bingo_Cards.xlsx"):
"""
Generates a set of bingo cards with correctly scaled cell dimensions and uniform thick black borders (1.5 px).
Parameters:
- terms (dict): Dictionary with "Game Events" and "Commercial Events" as keys and list of terms as values.
- num_cards (int): Number of bingo cards to generate.
- output_file (str): Output file name.
Returns:
- str: Path to the saved Excel file.
"""
workbook = Workbook()
workbook.remove(workbook.active) # Remove default sheet
# Define styling
header_font = Font(name="Aptos", bold=True, size=48)
cell_font = Font(name="Aptos", size=11)
free_space_font = Font(name="Aptos", bold=True, size=48)
alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
thick_black_border = Border(
left=Side(style="thick", color="000000"), right=Side(style="thick", color="000000"),
top=Side(style="thick", color="000000"), bottom=Side(style="thick", color="000000")
)
header_fill_red = PatternFill(start_color="8B0000", end_color="8B0000", fill_type="solid") # Dark Red
header_fill_blue = PatternFill(start_color="00008B", end_color="00008B", fill_type="solid") # Dark Blue
header_fill_default = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")
highlight_fill = PatternFill(start_color="00FF00", end_color="00FF00", fill_type="solid") # Green (for ☺ cells)
for i in range(num_cards):
# Select terms, allowing reuse if needed
game_events_sample = np.random.choice(terms["Game Events"], 12, replace=True)
commercial_events_sample = np.random.choice(terms["Commercial Events"], 12, replace=True)
free_space = ["☺"] # Use the ☺ symbol as the free space
all_items = np.concatenate([game_events_sample, commercial_events_sample, free_space])
np.random.shuffle(all_items)
grid = all_items.reshape(5, 5)
# Create a new sheet for the bingo card
sheet = workbook.create_sheet(title=f"Bingo Card {i+1}")
# Set headers
headers = ["B", "I", "N", "G", "O"]
for col_idx, header in enumerate(headers, start=1):
cell = sheet.cell(row=1, column=col_idx, value=header)
cell.font = header_font
cell.alignment = alignment
cell.border = thick_black_border
# Apply background colors for A1, C1, and E1
if col_idx in [1, 5]: # A1 and E1
cell.fill = header_fill_red
elif col_idx == 3: # C1
cell.fill = header_fill_blue
else:
cell.fill = header_fill_default
# Populate the bingo grid with thick black borders
for row_idx in range(5):
for col_idx in range(5):
cell_value = grid[row_idx, col_idx]
cell = sheet.cell(row=row_idx + 2, column=col_idx + 1, value=cell_value)
cell.font = free_space_font if cell_value == "☺" else cell_font
cell.alignment = alignment
cell.border = thick_black_border
# Apply conditional formatting for the ☺ cell
rule = CellIsRule(operator="equal", formula=['"☺"'], fill=highlight_fill)
sheet.conditional_formatting.add("A2:E6", rule)
# Set correct cell dimensions for 150px x 150px
for col in ["A", "B", "C", "D", "E"]:
sheet.column_dimensions[col].width = 20.5 # Approximate width for 150px
for row in range(1, 7):
sheet.row_dimensions[row].height = 112.5 # Approximate height for 150px
# Save file
excel_path = output_file
workbook.save(excel_path)
return excel_path
if __name__ == "__main__":
# Example usage
bingo_terms = {
"Game Events": ["Touchdown", "Field Goal", "Interception", "Fumble", "Penalty Flag Thrown"],
"Commercial Events": ["Car Commercial", "Beer Commercial", "Celebrity Cameo", "Movie Trailer", "Talking Animal"]
}
generate_bingo_cards(bingo_terms, num_cards=6, output_file="SuperBowl_Bingo_Cards.xlsx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment