#!/usr/bin/env python3 import os import sys from PIL import Image import zpl import csv # note: a page contains two labels in one row. This script therefore generates # two labels in one row, where each label describes two parts. PAGE_W = 100 PAGE_H = 24 # the labels are a bit too large for our part drawers, so we add some empty # space at the top that can be folded or cut away. TOP_OFFSET = 6 def add_part(l, idx, part_id, title): l.origin(3+idx*PAGE_W/4, TOP_OFFSET) #l.barcode('Q', f'http://partdb.im.zam.haus/scan/part/{part_id}', magnification=2, errorCorrection='L') l.barcode('X', f'http://partdb.im.zam.haus/scan/part/{part_id}', height=3) l.endorigin() text_left = 13+idx*PAGE_W/4 l.origin(text_left, TOP_OFFSET + 1) l.write_text(f'ID: {part_id}', char_height=3, char_width=3, line_width=PAGE_W/4*(1+idx)-text_left-2, justification='L', max_line=2) l.endorigin() maxlen = 30 if len(title) > maxlen: words = title.split(' ') if len(words) == 1: # only one word: cut off the end title = title[:maxlen] + "..." else: # concatenate words until the character limit is reached words_out = [] total_len = 0 for word in words: new_len = total_len + 1 + len(word) if new_len > maxlen: break print(f"Appending »{word}«", file=sys.stderr) words_out.append(word) total_len = new_len title = " ".join(words_out) + "..." text_left = 3+idx*PAGE_W/4 l.origin(text_left, TOP_OFFSET+11) l.write_text(title, char_height=3, char_width=3, line_width=PAGE_W/4*(1+idx)-text_left, justification='L', max_line=2) l.endorigin() part_ids = [] titles = [] with open('/tmp/export_Part_full.csv', 'r') as csvfile: dialect = csv.excel() dialect.delimiter = ';' reader = csv.DictReader(csvfile, dialect=dialect) for row in reader: part_ids.append(int(row['id'])) name = row['name'] name = name.replace('μ', 'u') name = name.replace('Ω', 'Ohm') titles.append(name) for i in range(len(part_ids)): if i % 4 == 0: l = zpl.Label(height=PAGE_H, width=PAGE_W, dpmm=8.0) if i % 2 == 1: print("Drawing Box…", file=sys.stderr) #l.origin(PAGE_W*i/4, TOP_OFFSET) l.origin(10, 10) l.draw_box(width=10*l.dpmm, height=10*l.dpmm, thickness=5) print(f"{part_ids[i]} => {titles[i]}", file=sys.stderr) add_part(l, i%4, part_ids[i], titles[i]) if i % 4 == 3: print(l.dumpZPL()) if len(part_ids) % 4 != 0: print(l.dumpZPL()) #l.preview()