Python之ReportLab绘制条形码和二维码的实例
作者:阿Bin先生 时间:2023-06-26 09:43:30
条形码和二维码
#引入所需要的基本包
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()
运行结果:
来源:http://blog.csdn.net/webzhuce/article/details/72972918
标签:Python,ReportLab,绘制,条形码,二维码
0
投稿
猜你喜欢
如何在页面中快捷地添加翻页按钮?
2010-06-26 12:33:00
SQL Server视图管理中的四个限制条件
2009-03-06 14:24:00
浅谈keras 模型用于预测时的注意事项
2022-10-16 13:23:04
python判断一个变量是否已经设置的方法
2022-06-09 19:33:15
PHP APC缓存配置、使用详解
2023-11-21 22:15:15
如何在Python里使用ChatGPT及ChatGPT是什么?注册方式?
2022-12-01 22:43:12
python 通过 pybind11 使用Eigen加速代码的步骤
2021-03-07 06:09:30
微软Silverlight技术魅力初体验
2008-11-05 11:16:00
perl控制流介绍(if条件,while,for循环,foreach)
2023-02-13 16:33:34
bootstrap-table后端分页功能完整实例
2024-04-29 13:11:44
Access中的模糊查询
2007-11-18 14:57:00
Keras目标检测mtcnn facenet搭建人脸识别平台
2023-09-28 07:02:01
Python实现XML文件解析的示例代码
2022-02-24 11:03:43
pytorch--之halfTensor的使用详解
2021-08-18 14:44:08
Python缩进和冒号详解
2023-04-21 16:22:11
4种JavaScript实现简单tab选项卡切换的方法
2024-02-23 10:25:51
Python爬取股票交易数据并可视化展示
2022-02-06 13:13:22
Python要求O(n)复杂度求无序列表中第K的大元素实例
2023-07-30 13:18:01
Linux+php+apache+oracle环境搭建之CentOS下安装Oracle数据库
2023-10-08 01:02:56
Python教程之成员和身份运算符的用法详解
2021-04-19 11:36:56