-1

I am trying to create barcodes in my app for each of the items

Here's the code:

from base64 import b64encode
from reportlab.lib import units
from reportlab.graphics import renderPM
from reportlab.graphics.barcode import createBarcodeDrawing
from reportlab.graphics.shapes import Drawing

def get_barcode(value, width, barWidth = 0.05 * units.inch, fontSize = 30):

    barcode = createBarcodeDrawing('Code128', value = value, barWidth = barWidth, fontSize = fontSize)

    drawing_width = width
    barcode_scale = drawing_width / barcode.width
    drawing_height = barcode.height * barcode_scale

    drawing = Drawing(drawing_width, drawing_height)
    drawing.scale(barcode_scale, barcode_scale)
    drawing.add(barcode, name='barcode')
    return drawing

sku = ['A100', 'A101', 'A102', 'A103', 'A104', 'A105', 'A106', 'A107', 'A108', 'A109']

for i in sku:
    barcode = get_barcode(value = i, width = 600)
    data = b64encode(renderPM.drawToString(barcode, fmt = 'PNG'))

How can I save every file at the desktop location i.e. r'C:\Users\Rahul\Desktop' with the filename as SKU Name ?

Rahul Sharma
  • 2,187
  • 6
  • 31
  • 76
  • Does this answer your question? [Save Image generated with Reportlab in my MEDIA folder (in Amazon S3)](https://stackoverflow.com/questions/24375868/save-image-generated-with-reportlab-in-my-media-folder-in-amazon-s3) – Ramon Medeiros Jan 22 '20 at 09:10
  • @RamonMedeiros Sorry, but no.. I want to store the barcodes in my local computer not on S3 – Rahul Sharma Jan 22 '20 at 09:13
  • Please, add appropriate tags and edit the title. It is too broad now. – Georgy Jan 22 '20 at 09:14
  • 1
    Yeah, but shows there how to save: `barcode.save(formats=['gif','pdf'],outDir=path_to_save,fnRoot=filename)` – Ramon Medeiros Jan 22 '20 at 09:14

1 Answers1

0

does this work for you (change the output path)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'g3n35i5'

import os
import base64
from reportlab.lib import units
from reportlab.graphics import renderPM
from reportlab.graphics.barcode import createBarcodeDrawing
from reportlab.graphics.shapes import Drawing


def get_barcode(value, width, barWidth=0.05 * units.inch, fontSize=30):
    barcode = createBarcodeDrawing('Code128', value=value, barWidth=barWidth, fontSize=fontSize)

    drawing_width = width
    barcode_scale = drawing_width / barcode.width
    drawing_height = barcode.height * barcode_scale

    drawing = Drawing(drawing_width, drawing_height)
    drawing.scale(barcode_scale, barcode_scale)
    drawing.add(barcode, name='barcode')
    return drawing


sku = ['A100', 'A101', 'A102', 'A103', 'A104', 'A105', 'A106', 'A107', 'A108', 'A109']

output_path = '/tmp/barcodes/'
if not os.path.exists(output_path):
    os.mkdir(output_path)

for i in sku:
    barcode = get_barcode(value=i, width=600)
    data = base64.b64encode(renderPM.drawToString(barcode, fmt='PNG'))
    filename = os.path.join(output_path, f"{i}.png")
    with open(filename, "wb") as fh:
        fh.write(base64.decodebytes(data))
g3n35i5
  • 485
  • 1
  • 4
  • 14