Enable Dark Mode!
By: Swapna

# How to Create a Barcode in Odoo 16

## Technical Odoo 16

Let’s discuss how to create a barcode in Odoo 16. As we all know, a barcode is a machine-readable visual representation of data. It represents numerical or alphabetic data using a pattern of parallel lines of various widths and spacings. The barcode scanner reads and analyses the information encoded in the barcode, which is then processed by a computer.
Let’s consider two cases,

## 1. Take an example of the Generates EAN13 Standard Barcode for Product.

`import mathfrom odoo import api, modelsclass ProductAutoBarcode(models.Model):    _inherit = 'product.product'    @api.model    def create(self, vals):        """generate barcode when create new product"""        res = super(ProductAutoBarcode, self).create(vals)        ean = generate_ean(str(res.id))        res.barcode = '21' + ean[2:]        return resdef ean_checksum(eancode):    """returns the checksum of an ean string of length 13, returns -1 if    the string has the wrong length"""    if len(eancode) != 13:        return -1    oddsum = 0    evensum = 0    eanvalue = eancode    reversevalue = eanvalue[::-1]    finalean = reversevalue[1:]    for i in range(len(finalean)):        if i % 2 == 0:            oddsum += int(finalean[i])        else:            evensum += int(finalean[i])    total = (oddsum * 3) + evensum    check = int(10 - math.ceil(total % 10.0)) % 10    return checkdef check_ean(eancode):    """returns True if eancode is a valid ean13 string, or null"""    if not eancode:        return True    if len(eancode) != 13:        return False    try:        int(eancode)    except:        return False    return ean_checksum(eancode)def generate_ean(ean):    """Creates and returns a valid ean13 from an invalid one"""    if not ean:        return "0000000000000"    product_identifier = '00000000000' + ean    ean = product_identifier[-11:]    check_number = check_ean(ean + '00')    return f'{ean}0{check_number}'class ProductTemplateAutoBarcode(models.Model):    _inherit = 'product.template'    @api.model    def create(self, vals_list):        """generate barcode number when create new product"""        templates = super(ProductTemplateAutoBarcode, self).create(vals_list)        ean = generate_ean(str(templates.id))        templates.barcode = '22' + ean[2:]        return templates`
Here, we are going to generate a barcode for a product. EAN-13 is the most widely recognized barcode in Europe, and it is used for basic product identification in supermarkets and other retail businesses. It is the European version of the American UPC-A barcode.

## 2. Next, we can check how to generate a barcode for the sequence and display it in the report in Odoo.

Print .py file:
`def generate_barcode(self):    data = {        'response': self.name,    }    return self.env.ref('module_name.action_generate_barcode').report_action(self,data=data)`
Print Button Xml:
`<header>            <button name="generate_barcode" string="Generate Barcode" type="object"/></header>`
Print barcode_action.xml:
`<?xml version="1.0" encoding="utf-8"?> <odoo>       <record id="action_generate_barcode" model="ir.actions.report">            <field name="name"> Barcode</field>            <field name="model">module.name</field>            <field name="report_type">qweb-pdf</field>            <field name="report_name">module_name.print_barcode</field>            <field name="report_file">module_name.print_barcode</field>      </record></odoo>`
Print barcode_template.xml:
`<? xml version ="1.0" encoding ="UTF-8" ?> <odoo> <template id ="print_barcode_custom" >     <t t-call ="web.basic_layout" >         <div class ="page" >             <div class = "col-md-6" >                 <img class ="barcode"                      t-att-src ="'/report/barcode/?barcode_type=%s & value=%s & width=%s & height=% s & humanreadable=1 & quiet=0' % ('Code128',response, 205, 67)"                      alt ="Barcode" />             </div>         </div>     </t> </template> </ odoo>`
This blog will give you an idea about barcode creation in Odoo 16.
To read more about managing barcode naming in Odoo 16 inventory, refer to our blog How to Manage Barcode Naming in Odoo 16 Inventory

If you need any assistance in odoo, we are online, please chat with us.

### Recent Posts

#### An Overview of Accounting Localization for Peru in Odoo 17

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Kochi, India - 682030.

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,