// Project Writeup

CSV Barcode Label Printer

A terminal tool that turns a one-value-per-line CSV into Code 128 labels on a Zebra ZP505. Case-exact encoding, whole-file validation before anything prints, and resumable runs.

Python ZPL Hardware macOS / CUPS Internal Tool

— The Problem

The Offline Container Label Printer does one job well: it prints the next number in a sequence. A new need came up that didn't fit that model. Someone hands over a list of values (part references, location codes, whatever the job calls for), and every one of them needs a scannable label that reads back exactly as written, upper and lower case included.

The constraints: the original Pi kiosk had to stay intact, since the sequential workflow may be needed again. The new tool should run from a Mac with the ZP505 plugged in over USB. A terminal is enough, with no web UI. And a bad row partway through a file must never leave a half-printed run that someone has to sort out by hand.

— Architecture

One stdlib-only Python script, no pip installs. It reads the whole file and checks every value before anything is sent. Only then does it hand ZPL to CUPS, which passes it untouched to the printer.

values.csv
   │
   ▼
read + validate      one column per row, printable ASCII only
   │                  (any error → list every bad line, print nothing)
   ▼
plan layout          pick bar width 3→2→1 dots, center barcode,
   │                  size human-readable text to fit
   ▼
preview + confirm    count, first/last values, notes   [y/N]
   │
   ▼
lp -d ZebraZP505 -o raw   one CUPS job per label
   │
   ▼
CUPS queue (Zebra ZPL sample driver) ──USB──▶ ZP505 · 203 dpi · 4" × 2"

Each label is a small, self-contained ZPL format. The print width and length are set on every label, so the result doesn't depend on whatever the printer has stored:

^XA^PW812^LL406^LH0,0
^FO238,109^BY3^BCN,127,N,N,N^FD>:ABC-001^FS        ← Code 128, Subset B forced
^FO16,248^A0N,48,48^FB780,1,0,C,0^FH\^FDABC-001^FS  ← centered, escaped text
^PQ1^XZ

A run supports --dry-run (writes a .zpl file, prints nothing), --test (one label), --header, --copies, --width/--height, and --start-line for resuming.

— Key Decisions

  • Code 128 Subset B, forced with >:
    Subset B covers all printable ASCII in both cases, which is what "exactly as provided" requires. Code 39 can't do lowercase without its extended mode. Zebra already defaults to Subset B when no start code is given, but stating it explicitly takes the guesswork out of any value.
  • Invocation codes for ^ > ~
    Inside a ZPL field, ^ and ~ are command prefixes and > is Zebra's Code 128 escape character. In the barcode data the encoder maps them to ><, >0, and >=, following Zebra's ^BC invocation table, so a value like a^b>c~d encodes literally instead of breaking the label.
  • Human-readable text as its own field
    The text line isn't the barcode's built-in interpretation line. It's a separate ^FB field, centered and hex-escaped with ^FH. That gives control over font size and centering, and lets the text shrink to fit long values.
  • Calculated centering and bar width
    ZPL has no "center this barcode" option. The width is computed directly as (11 × (chars + 2) + 13) × module, plus a 10-module quiet zone on each side. The script tries 3, then 2, then 1 dot per module. If even 1 doesn't fit, that value is rejected before the run starts, and 1-dot labels are flagged for a test scan.
  • Validate everything before the first label prints
    Rows with a stray extra column, characters Code 128 can't hold (accented letters, smart quotes, tabs), and values too long for the label are all caught up front and reported with line numbers. Quoted CSV values and the empty trailing cells Excel adds are handled. Leading and trailing spaces are kept and flagged rather than silently trimmed.
  • One CUPS job per label
    Batching would be marginally faster, but per-label jobs make resuming exact. If a job fails or the operator presses Ctrl-C, the script prints the precise --start-line to continue from, plus the cancel -a command for anything already queued. Throughput is set by the printer either way.
  • CUPS Zebra ZPL sample driver instead of a Raw queue
    The ContainerPi build had a deferred item about CUPS deprecating Raw queues. macOS ships CUPS's sample drv:///sample.drv/zebra.ppd, so the Mac queue uses a real driver entry, and the script sends with lp -o raw so CUPS filters never touch the ZPL. The queue keeps ContainerPi's name, ZebraZP505, so the printer can move between the two setups without reconfiguring either one.
  • A separate tool, not a fork of the kiosk
    This shares no files with ContainerPi. The Pi app, its SQLite counter, and its deploy zip are untouched, so the sequential workflow is one cable swap away.

— Lessons Learned

Printers remember things you didn't tell them

The first hand-sent ZPL test clipped its text on the right edge of a 4"-wide label. That test didn't set a print width, so the printer fell back on a narrower width stored in its configuration. A diagnostic label settled it in one print: ^PW812, a full-edge border box, and digits at every inch. The border showed edge to edge with 0 through 4 evenly spaced, which proved the stock was fine and the stored setting was the problem. Since then, every label the script generates sets its own ^PW/^LL.

Tune against paper, not numbers

The first layout capped the bars at 1.25", which works for scanning but looked heavy on a 2"-tall label. After the first real print, the cap was halved to 0.625". Because every layout value is a single constant in the config block, and the rest of the layout is calculated from those constants, that change was one line.

macOS CUPS works like Pi CUPS

The ContainerPi lessons carried straight over. A CUPS test page sends PostScript, which a ZPL printer drops, so the setup was verified by piping a one-line ZPL label into lp. sudo lpinfo -v lists the USB device URI, and the USB descriptor showed the printer reporting itself in ZPL mode before anything was sent.

— Stack

Language
Python 3 (stdlib only)
Interface
Terminal / argparse
Print path
CUPS · lp -o raw
Driver
CUPS Zebra ZPL sample PPD
Printer
Zebra ZP505 · 203 dpi · USB
Media
4" × 2" direct thermal
Symbology
Code 128, Subset B
Host
macOS (Linux-compatible)

— Status & Outcomes

Working on the Mac. The printer queue, the inch-ruler diagnostic, and test labels are verified, and the bar height was tuned after the first print. Operators get a predictable flow: drag a CSV into Terminal, dry-run it, print a five-row sample, then run the full file.

The sequential container label kiosk is preserved as-is. Between the two, the same ZP505 covers both counter-driven and list-driven labeling.