From af3677e13d0b013ebac4d3e2467a3979af9b6fd8 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sat, 24 Aug 2024 17:52:41 +0200 Subject: [PATCH] Add existing scripts for generating part labels --- mklabel_dual.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++ mklabel_single.py | 66 ++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 163 insertions(+) create mode 100755 mklabel_dual.py create mode 100755 mklabel_single.py create mode 100644 requirements.txt diff --git a/mklabel_dual.py b/mklabel_dual.py new file mode 100755 index 0000000..8df644f --- /dev/null +++ b/mklabel_dual.py @@ -0,0 +1,96 @@ +#!/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() diff --git a/mklabel_single.py b/mklabel_single.py new file mode 100755 index 0000000..9d9fd4d --- /dev/null +++ b/mklabel_single.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 + +import os +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 a single part. + +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/2, TOP_OFFSET) + l.barcode('Q', f'http://partdb.im.zam.haus/scan/part/{part_id}', magnification=3, errorCorrection='L') + l.endorigin() + + text_left = 3+idx*PAGE_W/2 + l.origin(text_left, TOP_OFFSET + 14) + l.write_text(f'ID: {part_id}', char_height=4, char_width=4, line_width=PAGE_W/2*(1+idx)-text_left-2, + justification='L') + l.endorigin() + + text_left = 18+idx*PAGE_W/2 + l.origin(text_left, TOP_OFFSET+1) + l.write_text(title, char_height=4, char_width=4, line_width=PAGE_W/2*(1+idx)-text_left-2, + justification='L', max_line=4) + 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 % 2 == 0: + l = zpl.Label(height=PAGE_H, width=PAGE_W, dpmm=8.0) + + print(f"{part_ids[i]} => {titles[i]}") + add_part(l, i%2, part_ids[i], titles[i]) + + if i % 2 == 1: + print(l.dumpZPL()) + +if len(part_ids) % 2 != 0: + print(l.dumpZPL()) + +#l.preview() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..436b245 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +zpl