-1

How can I generate barcode in python using alphanumeric strings? All the answers are for generating barcode using numbers only (using barcode package).

e.g. value : "ABC1234XYZ"

Referred https://www.geeksforgeeks.org/how-to-generate-barcode-in-python/ which talks about generating barcode in python. But a alphanumeric string cannot be used here.

1
  • 1
    "All the answers are for generating barcode using numbers only" then use a barcode format that isn't just for numbers. Commented Jan 8, 2022 at 8:12

2 Answers 2

2

From Python-barcode documentation, it states that it support different barcode type, including Code39 & Code128 etc which support alphanumeric values

https://python-barcode.readthedocs.io/en/stable/

Sign up to request clarification or add additional context in comments.

Comments

0

Intsall barcode library through pip or from Python Packages in Pycharm:

pip install barcode

Import library in your file:

import barcode
from barcode.writer import ImageWriter

The following function will create a barcode png and return its path. It will be a clean barcode without any text or numbers below it:

def generate_barcode(alphanum_string):
options = {
    'module_width': 0.3,  # Width of each module (bar)
    'module_height': 7.0,  # Height of each module (bar)
    'quiet_zone': 1.0,  # Width of the quiet zone on each side
    'font_size': 0,  # Font size for the text below the barcode (0 to remove text)
    'text_distance': 0.0,  # Distance between the barcode and the text
    'background': 'white',  # Background color
    'foreground': 'black',  # Foreground color
    'write_text': False  # Whether to write the text below the barcode
}

code128 = barcode.get_barcode_class('code128')
barcode_instance = code128(alphanum_string, writer=ImageWriter())

return barcode_instance.save("barcode", options=options)

Don't forget to give proper identation in file!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.