How to generate a EAN-13 barcode as a PNG file in Python

Who really has the time to manually code in functionality for barcode generation? What if there was a much easier way, one that would allow us to just instantly have our solution implemented? I’m going to show you just such a way. Here goes.

Our method will involve the use of an API, so let’s install its client now:

pip install cloudmersive-barcode-api-client

And now here is the function we will be using from our API, generate_barcode_ean13:

from __future__ import print_functionimport timeimport cloudmersive_barcode_api_clientfrom cloudmersive_barcode_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_barcode_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_barcode_api_client.GenerateBarcodeApi(cloudmersive_barcode_api_client.ApiClient(configuration))value = 'value_example' # str | Barcode value to generate fromtry:# Validates and generate a EAN-13 barcode as a PNG file, a type of 1D barcodeapi_response = api_instance.generate_barcode_ean13(value)pprint(api_response)except ApiException as e:print("Exception when calling GenerateBarcodeApi->generate_barcode_ean13: %s\n" % e)

And now we input our value, which will be transferred to the API and returned to us in PNG form as the completed barcode. There now, wasn’t that easy?

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store