import random from reportlab.lib.pagesizes import A4 from reportlab.lib import colors from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer, PageBreak from reportlab.lib.styles import getSampleStyleSheet from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont def generate_math_problem(): operation = random.choice(['+', '-', '*', '/']) if operation == '+': a, b = random.randint(0, 20), random.randint(0, 20) a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ') return f"{a_str} {operation} {b_str} =", a + b elif operation == '-': a, b = random.randint(0, 20), random.randint(0, 20) if a < b: a, b = b, a a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ') return f"{a_str} {operation} {b_str} =", a - b elif operation == '*': a, b = random.randint(0, 20), random.randint(0, 20) a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ') return f"{a_str} {operation} {b_str} =", a * b elif operation == '/': b = random.randint(1, 10) # Divisor a = b * random.randint(1, 20 // b) # Dividend (as a multiple of the divisor) a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ') return f"{a_str} {operation} {b_str} =", a // b def create_brain_jogging_pdf(filename): problems = [[generate_math_problem() for _ in range(4)] for _ in range(20)] data = [[problem[0] for problem in row] for row in problems] data_with_results = [[f"{problem[0]} {problem[1]}" for problem in row] for row in problems] doc = SimpleDocTemplate(filename, pagesize=A4) styles = getSampleStyleSheet() # Add title and time line title = Paragraph("Brain Jogging Exercises", styles['Heading1']) time_line = Paragraph("Time: _______________", styles['BodyText']) # Create a table for the title and time line # title_table = Table([[title, time_line]], colWidths=A4[0] / 2) title_table = Table([[title, time_line]], colWidths=(A4[0] / 2) * 0.75) title_table_style = TableStyle([ ('GRID', (0, 0), (-1, -1), 1, colors.white), ('ALIGN', (0, 0), (-1, -1), 'LEFT'), ('FONTNAME', (0, 0), (-1, -1), 'Helvetica'), ('FONTSIZE', (0, 0), (-1, -1), 12), ('BOTTOMPADDING', (0, 0), (-1, -1), 12), ]) title_table.setStyle(title_table_style) table = Table(data, colWidths=(A4[0] / 4) * 0.75) table_with_results = Table(data_with_results, colWidths=(A4[0] / 4) * 0.75) font_name = 'Consolas' # Change this to the desired name for your font font_path = './Consolas.ttf' # Change this to the path of the font file custom_font = TTFont(font_name, font_path) pdfmetrics.registerFont(custom_font) # Define table style table_style = TableStyle([ ('GRID', (0, 0), (-1, -1), 1, colors.white), ('ALIGN', (0, 0), (-1, -1), 'LEFT'), ('FONTNAME', (0, 0), (-1, -1), 'Consolas'), ('FONTSIZE', (0, 0), (-1, -1), 12), ('BOTTOMPADDING', (0, 0), (-1, -1), 16), ]) table.setStyle(table_style) table_with_results.setStyle(table_style) # Add tables to the PDF doc.build([ title_table, Spacer(1, 12), table, PageBreak(), # title, # time_line, title_table, Spacer(1, 12), table_with_results ]) if __name__ == "__main__": create_brain_jogging_pdf("brain-jogging.pdf")