Python之ReportLab繪製條形碼和二維碼的例項

Python之ReportLab繪製條形碼和二維碼的例項

條形碼和二維碼


#引入所需要的基本包
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("barcodes.pdf")
#呼叫函式生成條形碼和二維碼,並將canvas物件作為引數傳遞
createBarCodes(c)
#showPage函式:儲存當前頁的canvas
c.showPage()
#save函式:儲存檔案並關閉canvas
c.save()

執行結果:

以上這篇Python之ReportLab繪製條形碼和二維碼的例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援指令碼之家。

您可能感興趣的文章:

Python二維碼生成庫qrcode安裝和使用示例python實現二維碼掃碼自動登入淘寶分享Python文字生成二維碼例項使用python呼叫zxing庫生成二維碼圖片詳解Python基於QRCode實現生成二維碼的方法【下載,安裝,呼叫等】Python實現的二維碼生成小軟體Python使用QRCode模組生成二維碼例項詳解Python實現將文字生成二維碼的方法示例python生成二維碼的例項詳解Python及Django框架生成二維碼的方法分析