commit bf3da33050d9fbf94a8d6a690c2192cddb006543 Author: raven Date: Wed Jan 18 22:50:33 2023 +0100 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..559778b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin +lib +.idea +out \ No newline at end of file diff --git a/drinks.csv b/drinks.csv new file mode 100644 index 0000000..e2c28c9 --- /dev/null +++ b/drinks.csv @@ -0,0 +1,9 @@ +drink,filename +KitchenTschunk,tschunk +VirginTschunk,vir_tschunk +MoscowMule,moscowmule +VirginMoscowMule,vir_moscowmule +Schnaps2cl,schnaps2cl +Schnaps4cl,schnaps4cl +Likoer2cl,likoer2cl +GinTonic,gintonic diff --git a/main.py b/main.py new file mode 100644 index 0000000..207050b --- /dev/null +++ b/main.py @@ -0,0 +1,72 @@ +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!') diff --git a/pyvenv.cfg b/pyvenv.cfg new file mode 100644 index 0000000..4f6b30f --- /dev/null +++ b/pyvenv.cfg @@ -0,0 +1,8 @@ +home = /opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10 +implementation = CPython +version_info = 3.10.9.final.0 +virtualenv = 20.13.0 +include-system-site-packages = false +base-prefix = /opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10 +base-exec-prefix = /opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10 +base-executable = /opt/homebrew/bin/python3.10 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fae2ed9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +python-barcode +python-barcode[images] +fpdf