Python之ReportLab绘制条形码和二维码的实例

时间:2021-05-22

条形码和二维码

#引入所需要的基本包from reportlab.pdfgen import canvasfrom reportlab.graphics.barcode import code39, code128, code93from reportlab.graphics.barcode import eanbc, qr, uspsfrom reportlab.graphics.shapes import Drawing from reportlab.lib.units import mmfrom 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函数:保存当前页的canvasc.showPage()#save函数:保存文件并关闭canvasc.save()

运行结果:

以上这篇Python之ReportLab绘制条形码和二维码的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章