SoFunction
Updated on 2024-11-13

Python's ReportLab draw barcode and two-dimensional code examples

Barcode and QR code

# Introduce the required base packages
from  import canvas
from  import code39, code128, code93
from  import eanbc, qr, usps
from  import Drawing 
from  import mm
from  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 = (barcode_value)
  barcode_usps = ("50158-9999")
  codes = [barcode39, barcode39Std, barcode93, barcode128, barcode_usps]
  x = 1 * mm
  y = 285 * mm
  for code in codes:
    (c, x, y)
    y = y - 15 * mm
  # draw the eanbc8 code
  barcode_eanbc8 = eanbc.Ean8BarcodeWidget(barcode_value)
  d = Drawing(50, 10)
  (barcode_eanbc8)
  (d, c, 15, 555)
  # draw the eanbc13 code
  barcode_eanbc13 = eanbc.Ean13BarcodeWidget(barcode_value)
  d = Drawing(50, 10)
  (barcode_eanbc13)
  (d, c, 15, 465)
  # draw a QR code
  qr_code = ('/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])
  (qr_code)
  (d, c, 15, 405)
# Define the name of the pdf to be generated
c=("")
# Call the function to generate barcodes and QR codes, passing the canvas object as a parameter
createBarCodes(c)
#showPage function: save the current page of the canvas
()
The #save function: saves the file and closes the canvas.
()

Run results:

The above example of this Python's ReportLab to draw barcodes and QR codes is all I have to share with you, I hope it will give you a reference, and I hope you will support me more.