How to generate a UPC-A barcode as a PNG file in Python

I’m just going to come out and say it: barcode generation is annoying to set up. I know this because I’ve done it. But luckily for you I took those results and set them up as an API to save you the trouble. Let me demonstrate how to use it.

This API will need its client, so let’s install that first.

pip install cloudmersive-barcode-api-client

Now generate_barcode_upca can be called with this block of code I have for you here:

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 | UPC-A barcode value to generate fromtry:# Validate and generate a UPC-A barcode as a PNG file, a type of 1D barcodeapi_response = api_instance.generate_barcode_upca(value)pprint(api_response)except ApiException as e:print("Exception when calling GenerateBarcodeApi->generate_barcode_upca: %s\n" % e)

Now any value that we provide to generate_barcode_upca will be converted on the API’s end into our finished barcode. This same API has many other functions for various other barcode types, such as QR codes, as well as the ability to scan images for barcodes and return their values.

Image for post

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

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