initial commit...works, but needs tweaking

This commit is contained in:
2025-06-01 16:44:18 -07:00
commit f0a13d0cbb
3 changed files with 69 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*~

5
README.md Normal file
View File

@@ -0,0 +1,5 @@
partdb-labeler
==============
Connects to partdb.alfter.us, grabs a selected part, and formats a label to
be printed on an EPL2-compatible printer (such as the Zebra LP2844).

63
label.py Executable file
View File

@@ -0,0 +1,63 @@
#!/usr/bin/env python
import requests
from zebra import Zebra
import qrcode
from math import ceil, floor
from PIL import Image
import textwrap
from sys import argv
def filter(s, cp):
out=""
for i in s:
try:
i.encode(cp)
out=out+i
except:
pass
return out
label_width=2 # inches
label_height=1 #inches
id=argv[1]
api_key="tcp_673fc81f0b7837ca4c029fbd6536b27742eb8b742eba27bf547c8136dc6a84f8" # anonymous read-only
url=f"https://partdb.alfter.us/api/parts/{id}"
headers={}
headers["Accept"]="application/json"
headers["Authorization"]=f"Bearer {api_key}"
part=requests.get(url, headers=headers).json()
# print(f"name: {part["name"]}")
# print(f"description: {part["description"]}")
# print(f"manufacturer: {part["manufacturer"]["name"]}")
# print(f"MPN: {part["manufacturer_product_number"]}")
# print(f"IPN: {part["ipn"]}")
# print(f"URL: https://partdb.alfter.us/en/part/{id}")
z=Zebra("lp2844")
z.output(f"q{floor(203*label_width)}\n") #, label_height=(floor(203*label_height),0))
z.output("N\n")
qr = qrcode.QRCode(
box_size=4,
border=2,
)
qr.add_data(url)
qr.make(fit=True)
img = qr.make_image().copy()
padded=Image.new(mode="1", size=(8*ceil(img.width/8),img.height), color="white")
padded.paste(im=img, box=(0,0,img.width,img.height))
z.output(f"GW10,10,{ceil(padded.width/8)},{padded.height},{padded.tobytes().decode("cp437")}\n")
x=15+8*ceil(padded.width/8)
z.output(f"A{x},20,0,5,1,1,N,\"{part["ipn"]}\"\n")
z.output(f"A{x},80,0,2,1,1,N,\"{filter(part["manufacturer_product_number"], "cp437")}\"\n")
z.output(f"A{x},98,0,2,1,1,N,\"{filter(part["manufacturer"]["name"], "cp437")}\"\n")
y=116+28
for line in textwrap.wrap(filter(part["description"], "cp437"), width=floor((floor(203*label_width)-40)/10)):
z.output(f"A{10},{y},0,1,1,1,N,\"{line}\"\n")
y=y+14
z.output("P1\n")