import csv import os from barcode import Code128 from barcode.writer import ImageWriter from fpdf import FPDF # options code_height = 40 pdf_row_spacing = 40 # internal stuff pdf = FPDF() pdf_row = 0 pdf_colum = 0 def generate_barcode(filename, content): if not os.path.exists('out'): os.makedirs('out') with open('out/' + filename + '.png', "wb") as f: Code128(str(content), writer=ImageWriter()).write(f) def generate_barcode_username(filename, content): generate_barcode(filename, content + '\t') # we need a tab-character to select the 'strichliste' def pdf_add_barcode(imagepath): global pdf_row, pdf_colum, pdf_row_spacing # determine y-position y = 10 + (pdf_row * pdf_row_spacing) if y >= 251: print('maximum Code per Page reached. Creating new Page.') print('y: ' + str(y) + ' pdf_row: ' + str(pdf_row)) y = 10 pdf_row = 0 pdf.add_page() # determine x-position if pdf_colum == 0: x = 10 pdf_colum += 1 else: x = 100 pdf_colum = 0 # reset colum pdf_row += 1 # start a new row pdf.image(imagepath, x=x, y=y, h=code_height) if __name__ == '__main__': generate_barcode_username('raven', 'raven') pdf.add_page() pdf.set_font("Arial", size=15) filename = 'drinks.csv' with open(filename, 'r') as csvfile: datareader = csv.reader(csvfile) next(datareader, None) # skip the headers for row in datareader: print('Generating Code for:', row[0], 'to', row[1]) generate_barcode(row[1], row[0]) pdf_add_barcode('out/' + row[1] + '.png') pdf.output("out/barcodes.pdf") print() print('drink responsibly and') print('Don\'t drink and root!')