From 06e3874775baff2be232bdbbf268df77d111c4b2 Mon Sep 17 00:00:00 2001 From: tuetenk0pp Date: Sun, 30 Apr 2023 23:09:35 +0200 Subject: [PATCH] added ASN Script --- asn-barcodes.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 asn-barcodes.py diff --git a/asn-barcodes.py b/asn-barcodes.py new file mode 100644 index 0000000..7be32ab --- /dev/null +++ b/asn-barcodes.py @@ -0,0 +1,74 @@ +try: + import sys + import argparse + import barcode + from barcode import Code128 + from io import BytesIO + from reportlab.lib.pagesizes import A4 + from reportlab.lib.units import mm + from reportlab.pdfgen import canvas + from reportlab.lib.utils import ImageReader +except: + print("Unable to load libraries. Install them with 'pip install python-barcode reportlab'") + sys.exit(1) + +# Define arguments + +def check_positive(value): + try: + value = int(value) + if value <= 0: + raise argparse.ArgumentTypeError("{} is not a positive integer".format(value)) + except: + raise argparse.ArgumentTypeError("{} is not an integer".format(value)) + return value + +parser = argparse.ArgumentParser(prog="asn-barcodes.py", description="This tool can be used to generate ASN Barcodes for paperless-ngx.") +parser.add_argument('--version', action='version', version='%(prog)s 1.0') +parser.add_argument("position", help="Number of the first ASN-Barcode to generate", metavar="POSITION", type=check_positive) +parser.add_argument("count", help="Number of ASN-Barcodes to generate", metavar="COUNT", type=check_positive) +parser.add_argument("-o", "--output", default="asn-barcodes", dest="output", help="Destination file name", metavar="") +args = parser.parse_args() + +start = int(args.position) +end = int(args.position) + int(args.count) + +# Generate barcodes +barcodes = [] +for i in range(start, end): + barcode_value = 'ASN' + str(i).zfill(4) + barcode_image = Code128(barcode_value, writer=barcode.writer.ImageWriter()).render() + barcodes.append((barcode_value, barcode_image)) + +# Create PDF document with barcodes formatted for Avery L7871 Labels +pdf_file = args.output + ".pdf" +c = canvas.Canvas(pdf_file, pagesize=A4) +width, height = A4 +page_left_margin = 16.1 * mm +page_bottom_margin = 13.5 * mm +label_width = 25.4 * mm +label_height = 10 * mm +horizontal_gap = 0.25 * mm +vertical_gap = 0 * mm +rows = 27 +columns = 7 +x = page_left_margin +y = height - page_bottom_margin +for i, (barcode_value, barcode_image) in enumerate(barcodes): + if i > 0 and i % (rows * columns) == 0: + c.showPage() + x = page_left_margin + y = height - page_bottom_margin + # Convert barcode image to BytesIO object + barcode_image_file = BytesIO() + barcode_image.save(barcode_image_file, format='PNG') + barcode_image_file.seek(0) + # Draw barcode image on label + img = ImageReader(barcode_image_file) + c.drawImage(img, x, y - label_height, width=label_width, height=label_height) + x += label_width + horizontal_gap + if i % columns == columns - 1: + x = page_left_margin + y -= label_height + vertical_gap +c.save() +print(f"Generated {i + 1} barcodes with ASN from {start} to {end-1} in ./{pdf_file}") \ No newline at end of file