Wednesday 16 January 2019

Generate Barcode using Python


Step 1: Download and Install Python

Step 2: Open CMD prompt and run the following command to install flask and barcode module

>> pip install flask
>> pip install python-barcode
Step 3: Open the new text document and place the following code

import io

from flask import Flask, send_file, request

import barcode
from barcode.writer import ImageWriter

app=Flask(__name__)


@app.route('/search', methods=['GET'])
def search_data():
    query = request.args.get('query', '')
    ean = barcode.get('code128', query, writer=ImageWriter())  
    ean.default_writer_options['module_height'] = 5.0    
    ean.default_writer_options['write_text'] = False
    filename = ean.save(query)
    with open(filename, 'rb') as bites:
        return send_file(
                     io.BytesIO(bites.read()),
                     attachment_filename= query + '.png',
                     mimetype='image/png'
               )

if __name__ == "__main__":
    app.run(debug=True)  

Step 4: Save the file in .py format. (e.g. file_nm.py)

Step 5: Deploy this python web app in Restful web service.

1 comment: