Python Word bingo generator: Best output format for tables?

Hello!
I’m trying to create a word bingo generator workflow for my class. I can fill a txt with words and the scripts automatically pulls out the words and creates bingo cards out of it.

Now I’m looking for a good way to print those cards for my students. First I tried to output a ASCII table txt because I love keeping things simple but it didn’t work at all.
Now I’m creating a csv, open it with Numbers, edit the title row and column, press print, edit it again till it is in a printable form, print it out and cut it with my scissors. This is somehow ok, but it feels unnecessary complicated.

Can anyone think of a better way to export the cards into a readable standard format without using so much apps?

This is my script:

import random
import csv
import os

def read_words_from_file(file_path):
    with open(file_path, 'r') as file:
        words = [line.strip() for line in file]
    return words

def generate_bingo_cards(words, size, num_cards):
    cards = []
    for _ in range(num_cards):
        card = random.sample(words, size)
        cards.append(card)
    return cards

def write_bingo_cards_to_csv(cards, size, input_file):
    base_name = os.path.basename(input_file)
    output_file = os.path.splitext(base_name)[0] + "_bingo.csv"
    
    with open(output_file, 'w', newline='') as file:
        writer = csv.writer(file)
        for card in cards:
            rows = [card[i:i+5] for i in range(0, size, 5)]
            writer.writerows(rows)
            writer.writerow([])  # Add an empty row for visual separation

def main():
    file_path = input("Enter the path to the text file: ")
    words = read_words_from_file(file_path)
    
    size = 25  # Size of each bingo card (5x5)
    num_cards = 11  # Number of cards to generate
    
    if len(words) < size:
        print("Error: Insufficient number of words in the file.")
        return
    
    cards = generate_bingo_cards(words, size, num_cards)
    
    write_bingo_cards_to_csv(cards, size, file_path)
    print(f"Bingo cards have been generated and saved to '{os.path.splitext(file_path)[0]}_bingo.csv'.")

if __name__ == "__main__":
    main()

Perhaps write your own HTML in Python? When active in doing this sort of stuff, that is what I did. Relatively easy to do, and lets you do it as you see fit.

Then open the files in Browser and print.

edit: and with a little bit of CSS, can make your tables “pretty”.

2 Likes

hello and thank you for your reply.
I fear I’m overwhelmed by Python alone right now, I have to read into HTML right now. Thank you. I didn’t think about this being an option.

Basically you open a file for output, then construct the html as a text string and send to the open file. When done, close the file.

Once done it is automated and simple.

1 Like

HTML is nothing compared to Python. But if you don’t know HTML, you could do the same with the MarkDown table format. (However, if you’re writing code, going straight to HTML is likely to be easier than generating MarkDown.)

Yes, Python is the code to generate HTML which is then saved to a file that is easily read by a browser (or other). It’s not complicated. A good explanation is at How To Create Tables in HTML | DigitalOcean

I didn’t suggest MarkDown as then if one is left with a Markdown file, readers need a tool to read markdown. And markdown is just a super-set of HTML for writers to create HTML files, IMHO.