Elektronikecke-Labelgenerator/mklabel_single.py

67 lines
1.7 KiB
Python
Executable file

#!/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()