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数据结构之二叉树的遍历实例
Apr 29 Python
python实现八大排序算法(1)
Sep 14 Python
Python从零开始创建区块链
Mar 06 Python
PyTorch线性回归和逻辑回归实战示例
May 22 Python
Python中几种属性访问的区别与用法详解
Oct 10 Python
使用pandas把某一列的字符值转换为数字的实例
Jan 29 Python
详解Python字典的操作
Mar 04 Python
浅谈python标准库--functools.partial
Mar 13 Python
10分钟教你用python动画演示深度优先算法搜寻逃出迷宫的路径
Aug 12 Python
python读取图片的几种方式及图像宽和高的存储顺序
Feb 11 Python
基于python生成英文版词云图代码实例
May 16 Python
Python datetime 如何处理时区信息
Sep 02 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实现使用优酷土豆视频地址获取swf播放器分享地址
2014/06/05 PHP
php CI框架插入一条或多条sql记录示例
2014/07/29 PHP
Alliance vs Liquid BO3 第二场2.13
2021/03/10 DOTA
JScript内置对象Array中元素的删除方法
2007/03/08 Javascript
javascript Keycode对照表
2009/10/24 Javascript
js 页面刷新location.reload和location.replace的区别小结
2009/12/24 Javascript
jQuery 隐藏和显示 input 默认值示例
2014/06/03 Javascript
jQuery实现ctrl+enter(回车)提交表单
2015/10/19 Javascript
js实现网页图片延时加载 提升网页打开速度
2016/01/26 Javascript
JavaScript获取对象在页面中位置坐标的方法
2016/02/03 Javascript
JS深度拷贝Object Array实例分析
2016/03/31 Javascript
JS实现一个简单的日历
2017/02/22 Javascript
JS+CSS实现下拉刷新/上拉加载插件
2017/03/31 Javascript
angular实现spa单页面应用实例
2017/07/10 Javascript
javascript将json格式数组下载为excel表格的方法
2017/12/22 Javascript
JavaScript学习笔记之DOM基础操作实例小结
2019/01/09 Javascript
jquery+ajax实现上传图片并显示上传进度功能【附php后台接收】
2019/06/06 jQuery
JS document对象简单用法完整示例
2020/01/14 Javascript
JavaScript实现电灯开关小案例
2020/03/30 Javascript
利用webpack理解CommonJS和ES Modules的差异区别
2020/06/16 Javascript
实例讲解python函数式编程
2014/06/09 Python
举例讲解Python中is和id的用法
2015/04/03 Python
Python中zfill()方法的使用教程
2015/05/20 Python
python3 pathlib库Path类方法总结
2019/12/26 Python
Python爬取豆瓣数据实现过程解析
2020/10/27 Python
python实现模拟器爬取抖音评论数据的示例代码
2021/01/06 Python
详解HTML5布局和HTML5标签
2020/10/26 HTML / CSS
德国的大型美妆个护电商:Flaconi
2020/06/26 全球购物
应届毕业生的自我鉴定
2013/11/13 职场文书
水果超市创业计划书
2014/01/27 职场文书
小学生安全演讲稿
2014/04/25 职场文书
交通事故赔偿协议书怎么写
2014/10/04 职场文书
2014年体检中心工作总结
2014/12/23 职场文书
公司周年庆典致辞
2015/07/30 职场文书
nginx网站服务如何配置防盗链(推荐)
2021/03/31 Servers
Nginx内网单机反向代理的实现
2021/11/07 Servers