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!