python generate barcode

There are many third-party libraries for python to generate barcodes. I have roughly tried several commonly used libraries, and briefly talk about my feelings.
Let's talk about the results first. If you are using python3.x, it is recommended to use pyStrich.

pyBarcode

More information can be found, and it is easier to use.
Direct pip installation pip install pyBarcode
. Here is an example of EAN13 code

from barcode.writer import ImageWriter
from barcode.ean import EuropeanArticleNumber13
import barcode

Generate an EAN13 barcode and save it to the image. The default is png format without the suffix. The default self.format = 'PNG' in the ImageWriter initialization method

ean = EuropeanArticleNumber13("690123456789", writer=ImageWriter())
ean.save("image")
EAN = barcode.get_barcode_class("ean13")
ean = EAN("690123456789", writer=ImageWriter())
ean.save("image")

The above two methods are equivalent, and the effect is as follows
pyBarcode

But the disadvantage of pyBarcode is also obvious, that is, the start and terminator of EAN13 are not drawn.

ReportLab

pip installation is easy to operate pip install reportlab
on pdf, output the barcode to pdf, and directly upload the code

#引入所需要的基本包
from reportlab.pdfgen import canvas
from reportlab.graphics.barcode import code39, code128, code93
from reportlab.graphics.barcode import eanbc, qr, usps
from reportlab.graphics.shapes import Drawing 
from reportlab.lib.units import mm
from reportlab.graphics import renderPDF


def createBarCodes(c):
    barcode_value = "1234567890"

    barcode39 = code39.Extended39(barcode_value)
    barcode39Std = code39.Standard39(barcode_value, barHeight=20, stop=1)

    # code93 also has an Extended and MultiWidth version
    barcode93 = code93.Standard93(barcode_value)

    barcode128 = code128.Code128(barcode_value)
    # the multiwidth barcode appears to be broken 
    #barcode128Multi = code128.MultiWidthBarcode(barcode_value)

    barcode_usps = usps.POSTNET("50158-9999")

    codes = [barcode39, barcode39Std, barcode93, barcode128, barcode_usps]

    x = 1 * mm
    y = 285 * mm

    for code in codes:
        code.drawOn(c, x, y)
        y = y - 15 * mm

    # draw the eanbc8 code
    barcode_eanbc8 = eanbc.Ean8BarcodeWidget(barcode_value)
    d = Drawing(50, 10)
    d.add(barcode_eanbc8)
    renderPDF.draw(d, c, 15, 555)

    # draw the eanbc13 code
    barcode_eanbc13 = eanbc.Ean13BarcodeWidget(barcode_value)
    d = Drawing(50, 10)
    d.add(barcode_eanbc13)
    renderPDF.draw(d, c, 15, 465)

    # draw a QR code
    qr_code = qr.QrCodeWidget('http://blog.csdn.net/webzhuce')
    bounds = qr_code.getBounds()
    width = bounds[2] - bounds[0]
    height = bounds[3] - bounds[1]
    d = Drawing(45, 45, transform=[45./width,0,0,45./height,0,0])
    d.add(qr_code)
    renderPDF.draw(d, c, 15, 405)


#定义要生成的pdf的名称
c=canvas.Canvas("reportlab.pdf")
#调用函数生成条形码和二维码,并将canvas对象作为参数传递
createBarCodes(c)
#showPage函数:保存当前页的canvas
c.showPage()
#save函数:保存文件并关闭canvas
c.save()

The effect is as shown below, the ratio of EAN8 and EAN13 is a bit out of balance
ReportLab

huBarcode

Python3.x is not supported, development has been stopped in 2013, and
GitHub is replaced by pyStrich: https://github.com/hudora/huBarcode
python setup.py install

pyStrich

GitHub: https://github.com/mmulqueen/pyStrich
can be installed directly by pippip install pyStrich

from pystrich.ean13 import EAN13Encoder
encoder = EAN13Encoder("690123456789")
encoder.save("pyStrich.png")

The effect is as shown in the figure
pyStrich

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325952733&siteId=291194637