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基础教程之python消息摘要算法使用示例
Feb 10 Python
详解Python中heapq模块的用法
Jun 28 Python
Python 比较两个数组的元素的异同方法
Aug 17 Python
Python 查找字符在字符串中的位置实例
May 02 Python
python中sys.argv函数精简概括
Jul 08 Python
浅谈关于Python3中venv虚拟环境
Aug 01 Python
Python爬取数据保存为Json格式的代码示例
Apr 09 Python
关于python3中setup.py小概念解析
Aug 22 Python
修改 CentOS 6.x 上默认Python的方法
Sep 06 Python
python运用pygame库实现双人弹球小游戏
Nov 25 Python
Python3+SQLAlchemy+Sqlite3实现ORM教程
Feb 16 Python
python井字棋游戏实现人机对战
Apr 28 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
ftp类(example.php)
2006/10/09 PHP
php预定义常量
2006/12/25 PHP
一键删除顽固的空文件夹 软件下载
2007/01/26 PHP
php 数组的指针操作实现代码
2011/02/08 PHP
PHP投票系统防刷票判断流程分析
2012/02/04 PHP
基于initPHP的框架介绍
2013/04/18 PHP
提升PHP性能的21种方法介绍
2013/06/25 PHP
php+highchats生成动态统计图
2014/05/21 PHP
php继承中方法重载(覆盖)的应用场合
2015/02/09 PHP
js如何获取file控件的完整路径具体实现代码
2013/05/15 Javascript
关于jquery的多个选择器的使用示例
2013/10/18 Javascript
禁用JavaScript控制台调试的方法
2014/03/07 Javascript
express的中间件basicAuth详解
2014/12/04 Javascript
Js实现中国公民身份证号码有效性验证实例代码
2017/05/03 Javascript
详解AngularJS2 Http服务
2017/06/26 Javascript
使用vuex缓存数据并优化自己的vuex-cache
2018/05/30 Javascript
浅谈javascript中的prototype和__proto__的理解
2019/04/07 Javascript
vuex根据不同的用户权限展示不同的路由列表功能
2019/09/20 Javascript
jQuery HTML设置内容和属性操作实例分析
2020/05/20 jQuery
[27:28]Ti4 冒泡赛第二天 iG vs NEWBEE 1
2014/07/15 DOTA
[01:09]DOTA2次级职业联赛 - 99战队宣传片
2014/12/01 DOTA
python类继承用法实例分析
2014/10/10 Python
用Python实现随机森林算法的示例
2017/08/24 Python
python保存二维数组到txt文件中的方法
2018/11/15 Python
Python常用爬虫代码总结方便查询
2019/02/25 Python
python移位运算的实现
2019/07/15 Python
pytorch使用 to 进行类型转换方式
2020/01/08 Python
python小白学习包管理器pip安装
2020/06/09 Python
《老山界》教学反思
2014/04/08 职场文书
个人课题方案
2014/05/08 职场文书
英语教育专业自荐信
2014/05/29 职场文书
个人典型事迹材料
2014/12/30 职场文书
离婚起诉书怎么写
2015/05/19 职场文书
《观察物体》教学反思
2016/02/17 职场文书
golang 实现并发求和
2021/05/08 Golang
七个非常实用的Python工具包总结
2021/06/15 Python