mirror of
https://github.com/aborelis/ASN-Label-Generator.git
synced 2026-03-14 08:33:28 +00:00
cleanup
This commit is contained in:
parent
e2bb5dfc9c
commit
b8cdab0e4a
3 changed files with 160 additions and 144 deletions
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
__pycache__
|
||||||
|
venv
|
||||||
|
.idea
|
||||||
|
/*.pdf
|
||||||
|
out/*.pdf
|
||||||
299
asn-gen.py
299
asn-gen.py
|
|
@ -1,215 +1,226 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os
|
from functools import partial
|
||||||
import sys
|
|
||||||
|
|
||||||
import AveryLabels
|
import AveryLabels
|
||||||
from AveryLabels import labelInfo
|
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_qrcode import QRCodeImage
|
|
||||||
from reportlab.pdfgen import canvas
|
from reportlab.pdfgen import canvas
|
||||||
from reportlab.lib.colors import HexColor
|
from reportlab.lib.colors import HexColor
|
||||||
#from reportlab.graphics import shapes
|
from reportlab_qrcode import QRCodeImage
|
||||||
|
|
||||||
from clize import ArgumentError, Parameter, run
|
# from reportlab.graphics import shapes
|
||||||
|
|
||||||
|
from clize import run
|
||||||
|
|
||||||
|
|
||||||
from functools import partial
|
project_homepage = "https://github.com/aborelis/ASN-Label-Generator"
|
||||||
import inspect
|
|
||||||
|
|
||||||
project_homepage = 'https://github.com/aborelis/ASN-Label-Generator'
|
|
||||||
|
|
||||||
|
|
||||||
class LabelContext:
|
class LabelContext:
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
|
|
||||||
self.label = 4731
|
|
||||||
self.number =189
|
|
||||||
|
|
||||||
numDigits = 6
|
self.filename = None
|
||||||
self.firstAsn = 1
|
|
||||||
|
self.labeltype = 4731
|
||||||
|
self.number = 189
|
||||||
|
|
||||||
|
self.num_digits = 6
|
||||||
|
self.first_asn = 1
|
||||||
|
|
||||||
self.offset = 0
|
self.offset = 0
|
||||||
|
|
||||||
self.fontSize=2*mm
|
self.font_size = 2 * mm
|
||||||
self.qrSize = 0.9
|
self.qr_size = 0.9
|
||||||
self.qrMargin = 1*mm
|
self.qr_margin = 1 * mm
|
||||||
|
|
||||||
self.subLabelsX = 1
|
self.sub_labels_x = 1
|
||||||
self.subLabelsY = 1
|
self.sub_labels_y = 1
|
||||||
|
|
||||||
self.debug = False
|
|
||||||
self.positionHelper = False
|
|
||||||
|
|
||||||
|
self.debug = False
|
||||||
|
self.position_helper = False
|
||||||
|
|
||||||
self.barWidth=0
|
self.bar_width = 0
|
||||||
self.barColor= HexColor('#d2dede')
|
self.bar_color = HexColor("#d2dede")
|
||||||
self.highlightBarWidth = 0
|
self.highlight_bar_width = 0
|
||||||
self.highlightBarColor= HexColor('#d9a4a6')
|
self.highlight_bar_color = HexColor("#d9a4a6")
|
||||||
self.prefix='ASN'
|
self.prefix = "ASN"
|
||||||
|
|
||||||
self.__dict__.update(data)
|
self.__dict__.update(data)
|
||||||
|
|
||||||
self.currentASN = self.firstAsn
|
self.current_asn = self.first_asn
|
||||||
|
|
||||||
|
|
||||||
def incASN(self):
|
def incASN(self):
|
||||||
self.currentASN = self.currentASN + 1
|
self.current_asn = self.current_asn + 1
|
||||||
|
|
||||||
|
|
||||||
|
def render(context: LabelContext, c: canvas.Canvas, width: float, height: float):
|
||||||
|
|
||||||
|
sub_label_width = width / context.sub_labels_x
|
||||||
|
sub_labelheight = height / context.sub_labels_y
|
||||||
|
|
||||||
|
for i in range(context.sub_labels_x):
|
||||||
|
for j in range(context.sub_labels_y - 1, -1, -1): # no idea why inverted...
|
||||||
|
sub_x = sub_label_width * i
|
||||||
|
sub_y = sub_labelheight * j
|
||||||
|
|
||||||
|
c.saveState()
|
||||||
|
c.translate(sub_x, sub_y)
|
||||||
|
|
||||||
|
# barcode_value = f"ASN{currentASN:06d}"
|
||||||
|
barcode_value = context.prefix + str(context.current_asn).zfill(
|
||||||
|
context.num_digits
|
||||||
|
)
|
||||||
|
context.incASN()
|
||||||
|
|
||||||
|
qr = QRCodeImage(barcode_value, size=sub_labelheight * context.qr_size)
|
||||||
|
qr.drawOn(
|
||||||
|
c, x=context.qr_margin, y=sub_labelheight * ((1 - context.qr_size) / 2)
|
||||||
|
)
|
||||||
|
c.setFont("Helvetica", size=context.font_size)
|
||||||
|
c.drawString(
|
||||||
|
x=sub_labelheight,
|
||||||
|
y=(sub_labelheight - context.font_size) / 2,
|
||||||
|
text=barcode_value,
|
||||||
|
)
|
||||||
|
|
||||||
|
if context.bar_width > 0:
|
||||||
|
c.setFillColor(context.bar_color)
|
||||||
|
c.rect(
|
||||||
|
sub_label_width - context.bar_width,
|
||||||
|
0,
|
||||||
|
context.bar_width,
|
||||||
|
sub_labelheight,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
if context.highlight_bar_width > 0:
|
||||||
|
c.setFillColor(context.highlight_bar_color)
|
||||||
|
c.rect(
|
||||||
|
sub_label_width - context.bar_width - context.highlight_bar_width,
|
||||||
|
0,
|
||||||
|
context.highlight_bar_width,
|
||||||
|
sub_labelheight,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
if context.position_helper:
|
||||||
|
r = 0.1
|
||||||
|
d = 0
|
||||||
|
if context.debug:
|
||||||
|
r = 0.5
|
||||||
|
d = r
|
||||||
|
c.circle(x_cen=0 + d, y_cen=0 + d, r=r, stroke=1)
|
||||||
|
c.circle(x_cen=sub_label_width - d, y_cen=0 + d, r=r, stroke=1)
|
||||||
|
c.circle(x_cen=0 + d, y_cen=sub_labelheight - d, r=r, stroke=1)
|
||||||
|
c.circle(
|
||||||
|
x_cen=sub_label_width - d, y_cen=sub_labelheight - d, r=r, stroke=1
|
||||||
|
)
|
||||||
|
|
||||||
|
c.restoreState()
|
||||||
|
|
||||||
|
|
||||||
|
def generate(
|
||||||
def render(context: LabelContext , c: canvas.Canvas, width: float, height: float):
|
filename=None,
|
||||||
|
*,
|
||||||
|
labeltype: "l" = "4731",
|
||||||
subLabelWidth = width/context.subLabelsX
|
number: "n" = 189, # type: ignore
|
||||||
subLabelHeight = height/context.subLabelsY
|
offset: "o" = 0, # type: ignore
|
||||||
|
num_digits: "d" = 6, # type: ignore
|
||||||
for i in range(context.subLabelsX):
|
first_asn: "s" = 1, # type: ignore
|
||||||
for j in range(context.subLabelsY-1, -1, -1): # no idea why inverted...
|
font_size: "f" = "2mm", # type: ignore
|
||||||
subX = subLabelWidth*i
|
qr_size: "q" = 0.9, # type: ignore
|
||||||
subY = subLabelHeight*j
|
qr_margin: "m" = "1mm", # type: ignore
|
||||||
|
sub_labels_x: "lx" = 1, # type: ignore
|
||||||
c.saveState()
|
sub_labels_y: "ly" = 1, # type: ignore
|
||||||
c.translate(subX, subY)
|
debug=False,
|
||||||
|
position_helper=False,
|
||||||
# barcode_value = f"ASN{currentASN:06d}"
|
bar_width: "bw" = 0, # type: ignore
|
||||||
barcode_value = context.prefix+str(context.currentASN).zfill(context.numDigits)
|
bar_color: "bc" = "d2dede", # type: ignore
|
||||||
context.incASN()
|
highlight_bar_width: "hw" = 0, # type: ignore
|
||||||
|
highlight_bar_color: "hc" = "d9a4a6", # type: ignore
|
||||||
qr = QRCodeImage(barcode_value, size=subLabelHeight*context.qrSize)
|
prefix: "p" = "ASN", # type: ignore
|
||||||
qr.drawOn(c, x=context.qrMargin, y=subLabelHeight*((1-context.qrSize)/2))
|
):
|
||||||
c.setFont("Helvetica", size=context.fontSize)
|
|
||||||
c.drawString(x=subLabelHeight, y=(
|
|
||||||
subLabelHeight-context.fontSize)/2, text=barcode_value)
|
|
||||||
|
|
||||||
if context.barWidth > 0 :
|
|
||||||
c.setFillColor(context.barColor)
|
|
||||||
c.rect(subLabelWidth-context.barWidth, 0, context.barWidth, subLabelHeight,0, 1)
|
|
||||||
if context.highlightBarWidth > 0 :
|
|
||||||
c.setFillColor(context.highlightBarColor)
|
|
||||||
c.rect(subLabelWidth-context.barWidth-context.highlightBarWidth, 0, context.highlightBarWidth, subLabelHeight,0, 1)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if context.positionHelper:
|
|
||||||
r = 0.1
|
|
||||||
d = 0
|
|
||||||
if context.debug:
|
|
||||||
r = 0.5
|
|
||||||
d = r
|
|
||||||
c.circle(x_cen=0+d, y_cen=0+d, r=r, stroke=1)
|
|
||||||
c.circle(x_cen=subLabelWidth-d, y_cen=0+d, r=r, stroke=1)
|
|
||||||
c.circle(x_cen=0+d, y_cen=subLabelHeight-d, r=r, stroke=1)
|
|
||||||
c.circle(x_cen=subLabelWidth-d,
|
|
||||||
y_cen=subLabelHeight-d, r=r, stroke=1)
|
|
||||||
|
|
||||||
c.restoreState()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def generate( filename = None, *, labeltype:'l' = '4731',
|
|
||||||
number:'n'= 189,
|
|
||||||
offset:'o' = 0,
|
|
||||||
numDigits:'d'= 6,
|
|
||||||
firstAsn:'s' = 1,
|
|
||||||
fontSize:'f' = '2mm',
|
|
||||||
qrSize:'q' = 0.9,
|
|
||||||
qrMargin:'m' = '1mm',
|
|
||||||
|
|
||||||
subLabelsX:'lx' = 1,
|
|
||||||
subLabelsY:'ly' = 1,
|
|
||||||
|
|
||||||
debug = False ,
|
|
||||||
positionHelper = False,
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
barWidth:'bw'=0,
|
|
||||||
barColor:'bc'= 'd2dede',
|
|
||||||
highlightBarWidth:'hw'= 0 ,
|
|
||||||
highlightBarColor:'hc'= 'd9a4a6',
|
|
||||||
prefix:'p' = 'ASN'
|
|
||||||
):
|
|
||||||
"""ASN Label Generator
|
"""ASN Label Generator
|
||||||
|
|
||||||
:param filename: output filename of PDF file generated
|
:param filename: output filename of PDF file generated
|
||||||
:param labeltype: Type of label, e.g. 4731, get a list of supported labels with --labels
|
:param labeltype: Type of label, e.g. 4731, get a list of supported labels with --labels
|
||||||
:param number: number of labels to generate
|
:param number: number of labels to generate
|
||||||
|
|
||||||
:param offset: Number of labels to skip on the first sheet (e.g. already used)
|
:param offset: Number of labels to skip on the first sheet (e.g. already used)
|
||||||
:param numDigits: Number of digits for the ASN, e.g. 000001
|
:param num_digits: Number of digits for the ASN, e.g. 000001
|
||||||
:param firstAsn: First ASN to use, e.g. 100001
|
:param first_asn: First ASN to use, e.g. 100001
|
||||||
|
|
||||||
|
|
||||||
:param fontSize: Fontsize with a unit, e.g. 2mm, 0.4cm
|
:param font_size: Fontsize with a unit, e.g. 2mm, 0.4cm
|
||||||
:param qrSize: Size of the QR-Code as percentage of the label hight
|
:param qr_size: Size of the QR-Code as percentage of the label hight
|
||||||
:param qrMargin: Margin around the QR-Code with a unit, e.g. 1mm
|
:param qr_margin: Margin around the QR-Code with a unit, e.g. 1mm
|
||||||
|
|
||||||
|
:param sub_labels_x: How many labels to put on a phyical label horizontally
|
||||||
|
:param sub_labels_y: How many labels to put on a phyical label vertically
|
||||||
|
|
||||||
:param subLabelsX: How many labels to put on a phyical label horizontally
|
|
||||||
:param subLabelsY: How many labels to put on a phyical label vertically
|
|
||||||
|
|
||||||
:param debug: enable debug mode
|
:param debug: enable debug mode
|
||||||
:param positionHelper: enable position helpers, e.g. as cutting guides when using sub labels
|
:param position_helper: enable position helpers, e.g. as cutting guides when using sub labels
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
:param barWidth: Show a colored bar on the right of the label (0 = no bar)
|
|
||||||
:param barColor: Color of the bar, HEX notation
|
:param bar_width: Show a colored bar on the right of the label (0 = no bar)
|
||||||
:param highlightBarWidth: add a colored highlight bar on the right of the label (0 = no bar)
|
:param bar_color: Color of the bar, HEX notation
|
||||||
:param highlightBarColor: Color of the highlight bar, HEX notation
|
:param highlight_bar_width: add a colored highlight bar on the right of the label (0 = no bar)
|
||||||
|
:param highlight_bar_color: Color of the highlight bar, HEX notation
|
||||||
|
|
||||||
:param prefix: Prefix to the actual ASN number
|
:param prefix: Prefix to the actual ASN number
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
parm = locals()
|
parm = locals()
|
||||||
parm['fontSize'] = toLength(parm['fontSize'])
|
parm["font_size"] = toLength(parm["font_size"])
|
||||||
parm['qrMargin'] = toLength(parm['qrMargin'])
|
parm["qr_margin"] = toLength(parm["qr_margin"])
|
||||||
parm['barColor'] = HexColor('#'+parm['barColor'])
|
parm["bar_color"] = HexColor("#" + parm["bar_color"])
|
||||||
parm['highlightBarColor'] = HexColor('#'+parm['highlightBarColor'])
|
parm["highlight_bar_color"] = HexColor("#" + parm["highlight_bar_color"])
|
||||||
parm['labeltype'] = int(parm['labeltype'])
|
parm["labeltype"] = int(parm["labeltype"])
|
||||||
|
|
||||||
if parm['filename'] == None:
|
if parm["filename"] is None:
|
||||||
parm['filename']= 'label-'+str(parm['labeltype'])+'-'+parm['prefix']+'-'+str(parm['firstAsn']).zfill(parm['numDigits'])+'-'+str(parm['firstAsn']+parm['number']).zfill(parm['numDigits'])+'.pdf'
|
parm["filename"] = (
|
||||||
|
"label-"
|
||||||
|
+ str(parm["labeltype"])+ "-"
|
||||||
|
+ parm["prefix"]+ "-"
|
||||||
|
+ str(parm["first_asn"]).zfill(parm["num_digits"])+ "-"
|
||||||
|
+ str(parm["first_asn"] + parm["number"]).zfill(parm["num_digits"])
|
||||||
|
+ ".pdf"
|
||||||
|
)
|
||||||
|
|
||||||
context = LabelContext(parm)
|
context = LabelContext(parm)
|
||||||
|
|
||||||
|
|
||||||
label = AveryLabels.AveryLabel(context.labeltype)
|
label = AveryLabels.AveryLabel(context.labeltype)
|
||||||
|
|
||||||
label.debug = context.debug
|
label.debug = context.debug
|
||||||
|
|
||||||
label.open(context.filename)
|
label.open(context.filename)
|
||||||
|
|
||||||
render_func = partial(render, context)
|
render_func = partial(render, context)
|
||||||
label.render(render_func, count=context.number, offset=context.offset)
|
label.render(render_func, count=context.number, offset=context.offset)
|
||||||
label.close()
|
label.close()
|
||||||
|
|
||||||
print
|
|
||||||
print(f"Output written to {context.filename}")
|
print(f"Output written to {context.filename}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def labels():
|
def labels():
|
||||||
""" Shows a list of supported labels
|
"""Shows a list of supported labels"""
|
||||||
"""
|
print("Supported Labels: " + ", ".join(map(str, labelInfo.keys())))
|
||||||
print('Supported Labels: '+', '.join(map(str,labelInfo.keys())))
|
|
||||||
|
|
||||||
|
|
||||||
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():
|
||||||
run(generate, alt=[labels,version])
|
run(generate, alt=[labels, version])
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
|
||||||
0
out/empty
Normal file
0
out/empty
Normal file
Loading…
Add table
Add a link
Reference in a new issue