#!/usr/bin/env python3 import argparse import math import fpdf import strictyaml class Chart: COLUMNS=4 ROWS=16 def __init__(self): self.parser = self.get_parser() self.args = self.parser.parse_args() self.data = strictyaml.load(self.args.chart_data.read()).data def get_parser(self): parser = argparse.ArgumentParser() parser.add_argument( "chart_data", type=argparse.FileType("r") ) return parser def get_arranged_cells(self): per_page = self.COLUMNS * self.ROWS padded = self.data["colours"] + [{}] * ( per_page - len(self.data["colours"]) % per_page ) pages = [ padded[i * per_page:i * per_page + per_page] for i in range(len(padded) // per_page) ] cells = [ [ [p[i + self.ROWS * j] for j in range(self.COLUMNS)] for i in range(len(p) // self.COLUMNS) ] for p in pages ] return(cells) def write_pdf(self, fname): pdf = fpdf.FPDF(format="A5") for p_num, page in enumerate(self.get_arranged_cells()): pdf.add_page() pdf.set_font("helvetica", size=12) pdf.cell(64, 8, self.data["title"], align="C") pdf.ln() pdf.set_font("helvetica", "B", 12) for j in range(self.COLUMNS): pdf.cell(16, 8, str(j + p_num * self.COLUMNS),) pdf.cell(16, 8, "") pdf.ln() for row in page: for cell in row: if cell: pdf.set_fill_color(255,255,255) if cell.get("bg"): pdf.set_fill_color(*map(int, cell["bg"].split())) pdf.cell(16, 8, "", border=1, fill=True) pdf.cell(16, 8, cell["label"], ) pdf.ln(10) pdf.output(fname) if __name__ == "__main__": Chart().write_pdf("asd.pdf")