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标准库defaultdict模块使用示例
Apr 28 Python
21行Python代码实现拼写检查器
Jan 25 Python
Python实现TCP协议下的端口映射功能的脚本程序示例
Jun 14 Python
Python调用微信公众平台接口操作示例
Jul 08 Python
python实现协同过滤推荐算法完整代码示例
Dec 15 Python
连接pandas以及数组转pandas的方法
Jun 28 Python
python如何给字典的键对应的值为字典项的字典赋值
Jul 05 Python
pandas中DataFrame修改index、columns名的方法示例
Aug 02 Python
Django认证系统user对象实现过程解析
Mar 02 Python
python raise的基本使用
Sep 10 Python
Python文件操作及内置函数flush原理解析
Oct 13 Python
Python NumPy灰度图像的压缩原理讲解
Aug 04 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 实现多服务器共享 SESSION 数据
2009/08/15 PHP
php session 写入数据库
2016/02/13 PHP
php使用正则表达式去掉html中的注释方法
2016/11/03 PHP
php中Ioc(控制反转)和Di(依赖注入)
2017/05/07 PHP
JavaScript 对象的属性和方法4种不同的类型
2010/03/19 Javascript
用Mootools获得操作索引的两种方法分享
2011/12/12 Javascript
js对文章内容进行分页示例代码
2014/03/05 Javascript
js实现屏幕自适应局部代码分享
2015/01/30 Javascript
jQuery给动态添加的元素绑定事件的方法
2015/03/09 Javascript
js实现创建删除html元素小结
2015/09/30 Javascript
jQuery中Find选择器用法示例
2016/09/21 Javascript
深入理解JavaScript中的for循环
2017/02/07 Javascript
JS交互点击WKWebView中的图片实现预览效果
2018/01/05 Javascript
vue2.0 自定义 饼状图 (Echarts)组件的方法
2018/03/02 Javascript
vue单页缓存方案分析及实现
2018/09/25 Javascript
如何使用VuePress搭建一个类型element ui文档
2019/02/14 Javascript
微信公众号生成新浪短网址的实现(快速生成)
2019/08/18 Javascript
vue+node 实现视频在线播放的实例代码
2020/10/19 Javascript
[02:17]2016完美“圣”典风云人物:Sccc专访
2016/12/03 DOTA
Flask框架各种常见装饰器示例
2018/07/17 Python
使用OpenCV获取图像某点的颜色值,并设置某点的颜色
2020/06/02 Python
Python多线程threading创建及使用方法解析
2020/06/17 Python
Python包和模块的分发详细介绍
2020/06/19 Python
python用Tkinter做自己的中文代码编辑器
2020/09/07 Python
NOTINO英国:在线购买美容和香水
2020/02/25 全球购物
迪卡侬(Decathlon)加拿大官网:源自法国的运动专业超市
2020/11/22 全球购物
给领导的致歉信范文
2014/01/13 职场文书
升旗仪式主持词
2014/03/19 职场文书
邹越感恩父母演讲稿
2014/08/28 职场文书
2014年有孩子的离婚协议书范本
2014/10/08 职场文书
2014年医院工作总结
2014/11/20 职场文书
2015年外贸业务员工作总结范文
2015/05/23 职场文书
人与自然的观后感
2015/06/18 职场文书
2019年公司卫生管理制度样本
2019/08/21 职场文书
用Python实现Newton插值法
2021/04/17 Python
MYSQL优化之数据表碎片整理详解
2022/04/03 MySQL