This commit is contained in:
Aborelis 2024-05-14 02:06:55 +02:00
parent b8cdab0e4a
commit a9337407b7
2 changed files with 48 additions and 41 deletions

View file

@ -23,27 +23,25 @@ from reportlab.lib.units import mm, cm
labelInfo = { labelInfo = {
# 22x 32mm x 10mm mini labels # 22x 32mm x 10mm mini labels
3044: ( 2, 11, (32, 10), (2,2), (1, 1), (66.5*mm, 120.5*mm)), 3044: (2, 11, (32, 10), (2, 2), (1, 1), (66.5 * mm, 120.5 * mm)),
# 189x 25.4mm x 10mm mini labels # 189x 25.4mm x 10mm mini labels
4731: ( 7, 27, (25.4*mm, 10*mm), (2.5*mm, 0), (9*mm, 13.5*mm), A4), 4731: (7, 27, (25.4 * mm, 10 * mm), (2.5 * mm, 0), (9 * mm, 13.5 * mm), A4),
# 2.6 x 1 address labels # 2.6 x 1 address labels
5160: ( 3, 10, (187, 72), (11, 0), (14, 36), A4), 5160: (3, 10, (187, 72), (11, 0), (14, 36), A4),
5161: ( 2, 10, (288, 72), (0, 0), (18, 36), A4), 5161: (2, 10, (288, 72), (0, 0), (18, 36), A4),
# 4 x 2 address labels # 4 x 2 address labels
5163: ( 2, 5, (288, 144), (0, 0), (18, 36), A4), 5163: (2, 5, (288, 144), (0, 0), (18, 36), A4),
# 1.75 x 0.5 return address labels # 1.75 x 0.5 return address labels
5167: ( 4, 20, (126, 36), (0, 0), (54, 36), A4), 5167: (4, 20, (126, 36), (0, 0), (54, 36), A4),
# 3.5 x 2 business cards # 3.5 x 2 business cards
5371: ( 2, 5, (252, 144), (0, 0), (54, 36), A4), 5371: (2, 5, (252, 144), (0, 0), (54, 36), A4),
# 48x 45.7x21.2mm # 48x 45.7x21.2mm
4778: (4, 12, (45.7*mm, 21.2*mm), (0.25*cm, 0), (1.1*cm, 2*cm), A4), 4778: (4, 12, (45.7 * mm, 21.2 * mm), (0.25 * cm, 0), (1.1 * cm, 2 * cm), A4),
# APLI 100984 40x 52.5x29.7mm # APLI 100984 40x 52.5x29.7mm
100984: (4, 10, (52.5*mm, 29.7*mm), (0, 0), (0, 0), A4), 100984: (4, 10, (52.5 * mm, 29.7 * mm), (0, 0), (0, 0), A4),
} }
class AveryLabel: class AveryLabel:
def __init__(self, label, **kwargs): def __init__(self, label, **kwargs):
@ -51,42 +49,46 @@ class AveryLabel:
self.across = data[0] self.across = data[0]
self.down = data[1] self.down = data[1]
self.size = data[2] self.size = data[2]
self.labelsep = self.size[0]+data[3][0], self.size[1]+data[3][1] self.labelsep = self.size[0] + data[3][0], self.size[1] + data[3][1]
self.margins = data[4] self.margins = data[4]
self.topDown = True self.top_down = True
self.debug = False self.debug = False
self.pagesize = data[5] self.pagesize = data[5]
self.position = 0 self.position = 0
self.__dict__.update(kwargs) self.__dict__.update(kwargs)
def open(self, filename): def open(self, filename):
self.canvas = canvas.Canvas( filename, pagesize=self.pagesize ) """open a canvas for the file"""
self.canvas = canvas.Canvas(filename, pagesize=self.pagesize)
if self.debug: if self.debug:
self.canvas.setPageCompression( 0 ) self.canvas.setPageCompression(0)
self.canvas.setLineJoin(1) self.canvas.setLineJoin(1)
self.canvas.setLineCap(1) self.canvas.setLineCap(1)
def topLeft(self, x=None, y=None): def top_left(self, x=None, y=None):
""" return top left"""
if x is None: if x is None:
x = self.position x = self.position
if y is None: if y is None:
if self.topDown: if self.top_down:
x,y = divmod(x, self.down) x, y = divmod(x, self.down)
else: else:
y,x = divmod(x, self.across) y, x = divmod(x, self.across)
return ( return (
self.margins[0]+x*self.labelsep[0], self.margins[0] + x * self.labelsep[0],
self.pagesize[1] - self.margins[1] - (y+1)*self.labelsep[1] self.pagesize[1] - self.margins[1] - (y + 1) * self.labelsep[1],
) )
def advance(self): def advance(self):
""" move to next position"""
self.position += 1 self.position += 1
if self.position == self.across * self.down: if self.position == self.across * self.down:
self.canvas.showPage() self.canvas.showPage()
self.position = 0 self.position = 0
def close(self): def close(self):
"""save and close canvas"""
if self.position: if self.position:
self.canvas.showPage() self.canvas.showPage()
self.canvas.save() self.canvas.save()
@ -99,35 +101,36 @@ class AveryLabel:
# Or, pass a callable and an iterator. We'll do one label # Or, pass a callable and an iterator. We'll do one label
# per iteration of the iterator. # per iteration of the iterator.
def render( self, thing, count, offset=0, *args ): def render(self, thing, count, offset=0, *args):
""" render loop"""
assert callable(thing) or isinstance(thing, str) assert callable(thing) or isinstance(thing, str)
if isinstance(count, Iterator): if isinstance(count, Iterator):
return self.render_iterator( thing, count ) return self.render_iterator(thing, count)
canv = self.canvas canv = self.canvas
for i in range(offset+count): for i in range(offset + count):
if i >= offset: if i >= offset:
canv.saveState() canv.saveState()
canv.translate( *self.topLeft() ) canv.translate(*self.top_left())
if self.debug: if self.debug:
canv.setLineWidth( 0.25 ) canv.setLineWidth(0.25)
canv.rect( 0, 0, self.size[0], self.size[1] ) canv.rect(0, 0, self.size[0], self.size[1])
if callable(thing): if callable(thing):
thing( canv, self.size[0], self.size[1], *args ) thing(canv, self.size[0], self.size[1], *args)
elif isinstance(thing, str): elif isinstance(thing, str):
canv.doForm(thing) canv.doForm(thing)
canv.restoreState() canv.restoreState()
self.advance() self.advance()
def render_iterator( self, func, iterator ): def render_iterator(self, func, iterator):
""" render loop for iterator """
canv = self.canvas canv = self.canvas
for chunk in iterator: for chunk in iterator:
canv.saveState() canv.saveState()
canv.translate( *self.topLeft() ) canv.translate(*self.top_left())
if self.debug: if self.debug:
canv.setLineWidth( 0.25 ) canv.setLineWidth(0.25)
canv.rect( 0, 0, self.size[0], self.size[1] ) canv.rect(0, 0, self.size[0], self.size[1])
func( canv, self.size[0], self.size[1], chunk ) func(canv, self.size[0], self.size[1], chunk)
canv.restoreState() canv.restoreState()
self.advance() self.advance()

View file

@ -2,8 +2,7 @@
from functools import partial from functools import partial
import AveryLabels
from AveryLabels import labelInfo
from reportlab.lib.units import mm from reportlab.lib.units import mm
from reportlab.lib.units import toLength from reportlab.lib.units import toLength
from reportlab.pdfgen import canvas from reportlab.pdfgen import canvas
@ -14,8 +13,11 @@ from reportlab_qrcode import QRCodeImage
from clize import run from clize import run
import AveryLabels
from AveryLabels import labelInfo
project_homepage = "https://github.com/aborelis/ASN-Label-Generator"
PROJECT_HOMEPAGE = "https://github.com/aborelis/ASN-Label-Generator"
class LabelContext: class LabelContext:
@ -57,6 +59,7 @@ class LabelContext:
def render(context: LabelContext, c: canvas.Canvas, width: float, height: float): def render(context: LabelContext, c: canvas.Canvas, width: float, height: float):
""" renders one label onto the provided canvas. To be used with AveryLabel. """
sub_label_width = width / context.sub_labels_x sub_label_width = width / context.sub_labels_x
sub_labelheight = height / context.sub_labels_y sub_labelheight = height / context.sub_labels_y
@ -215,10 +218,11 @@ def labels():
def version(): def version():
"""Show the version""" """Show the version"""
return "ASN Label Generator - version 0.1 \n" + project_homepage return "ASN Label Generator - version 0.1 \n" + PROJECT_HOMEPAGE
def main(): def main():
"""Main function - entry point"""
run(generate, alt=[labels, version]) run(generate, alt=[labels, version])