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


Posted in Python onJanuary 15, 2018

条形码和二维码

#引入所需要的基本包
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之ReportLab绘制条形码和二维码的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python正则表达式re模块详解
Jun 25 Python
python:socket传输大文件示例
Jan 18 Python
python计算auc指标实例
Jul 13 Python
Python编程之string相关操作实例详解
Jul 22 Python
Python实现字符串反转的常用方法分析【4种方法】
Sep 30 Python
Python利用matplotlib.pyplot绘图时如何设置坐标轴刻度
Apr 09 Python
python 产生token及token验证的方法
Dec 26 Python
使用python绘制温度变化雷达图
Oct 18 Python
Python timer定时器两种常用方法解析
Jan 20 Python
python装饰器三种装饰模式的简单分析
Sep 04 Python
用sleep间隔进行python反爬虫的实例讲解
Nov 30 Python
使用tensorflow 实现反向传播求导
May 26 Python
Tornado高并发处理方法实例代码
Jan 15 #Python
使用Python实现windows下的抓包与解析
Jan 15 #Python
Python实现可获取网易页面所有文本信息的网易网络爬虫功能示例
Jan 15 #Python
Python操作mysql数据库实现增删查改功能的方法
Jan 15 #Python
使用python编写简单的小程序编译成exe跑在win10上
Jan 15 #Python
python逆向入门教程
Jan 15 #Python
Python3一行代码实现图片文字识别的示例
Jan 15 #Python
You might like
PHP实现文件上传与下载实例与总结
2016/03/13 PHP
javascript+dom树型菜单类,希望朋友们一起进步
2007/05/03 Javascript
js同时按下两个方向键
2007/12/01 Javascript
jquery ajax应用中iframe自适应高度问题解决方法
2014/04/12 Javascript
JavaScript中如何通过arguments对象实现对象的重载
2014/05/12 Javascript
JS控制输入框内字符串长度
2014/05/21 Javascript
jQuery学习笔记之 Ajax操作篇(二) - 数据传递
2014/06/23 Javascript
用js提交表单解决一个页面有多个提交按钮的问题
2014/09/01 Javascript
Js和JQuery获取鼠标指针坐标的实现代码分享
2015/05/25 Javascript
JS数组array元素的添加和删除方法代码实例
2015/06/01 Javascript
学习JavaScript设计模式(封装)
2015/11/26 Javascript
jQuery点击改变class并toggle及toggleClass()方法定义用法
2015/12/11 Javascript
详细谈谈javascript的对象
2016/07/31 Javascript
js实现图片切换(动画版)
2016/12/25 Javascript
微信小程序中使用Promise进行异步流程处理的实例详解
2017/08/17 Javascript
基于react组件之间的参数传递(详解)
2017/09/05 Javascript
js实现以最简单的方式将数组元素添加到对象中的方法
2017/12/20 Javascript
关于Vue在ie10下空白页的debug小结
2018/05/02 Javascript
js input输入百分号保存数据库失败的解决方法
2018/05/26 Javascript
基于bootstrap页面渲染的问题解决方法
2018/08/09 Javascript
vue实现与安卓、IOS交互的方法
2018/11/02 Javascript
[58:23]LGD vs TNC 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
使用requests库制作Python爬虫
2018/03/25 Python
pyhton列表转换为数组的实例
2018/04/04 Python
ERLANG和PYTHON互通实现过程详解
2019/07/05 Python
python是否适合网页编程详解
2019/10/04 Python
Pycharm远程连接服务器并实现代码同步上传更新功能
2020/02/25 Python
Pycharm安装并配置jupyter notebook的实现
2020/05/18 Python
使用CSS Grid布局实现网格的流动
2014/12/30 HTML / CSS
NHL官方在线商店:Shop.NHL.com
2020/05/01 全球购物
介绍一下grep命令的使用
2015/06/12 面试题
焊接专业毕业生求职信
2013/10/01 职场文书
大学生个人自荐信
2014/02/24 职场文书
营销总监岗位职责范本
2014/02/26 职场文书
2014年护士工作总结范文
2014/11/11 职场文书
DIY胆机必读:各国电子管评价
2022/04/06 无线电