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实现windows下模拟按键和鼠标点击的方法
Mar 13 Python
python dict 字典 以及 赋值 引用的一些实例(详解)
Jan 20 Python
python制作小说爬虫实录
Aug 14 Python
python实现微信小程序自动回复
Sep 10 Python
python+unittest+requests实现接口自动化的方法
Nov 29 Python
Python如何将图像音视频等资源文件隐藏在代码中(小技巧)
Feb 16 Python
python使用梯度下降和牛顿法寻找Rosenbrock函数最小值实例
Apr 02 Python
python中使用input()函数获取用户输入值方式
May 03 Python
如何导出python安装的所有模块名称和版本号到文件中
Jun 05 Python
python 实现汉诺塔游戏
Nov 28 Python
解决pytorch 保存模型遇到的问题
Mar 03 Python
Python虚拟环境virtualenv是如何使用的
Jun 20 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二分法查找数组是否包含某一元素
2013/05/23 PHP
解析php扩展php_curl.dll不加载的解决方法
2013/06/26 PHP
preg_match_all使用心得分享
2014/01/31 PHP
PHP内置的Math函数效率测试
2014/12/01 PHP
PHP中md5()函数的用法讲解
2019/03/30 PHP
PHP连接SQL Server的方法分析【基于thinkPHP5.1框架】
2019/05/06 PHP
JavaScript 判断指定字符串是否为有效数字
2010/05/11 Javascript
JavaScript去掉数组中的重复元素
2011/01/13 Javascript
jQuery版Tab标签切换
2011/03/16 Javascript
jquery 中的each()跳出循环的语句
2014/05/23 Javascript
仿淘宝TAB切换搜索框搜索切换的相关内容
2014/09/21 Javascript
实例讲解javascript注册事件处理函数
2016/01/09 Javascript
深入理解JS正则表达式---分组
2016/07/18 Javascript
JavaScript实现的数字与字符串转换功能示例
2017/08/23 Javascript
Vue组件之自定义事件的功能图解
2018/02/01 Javascript
webpack下实现动态引入文件方法
2018/02/22 Javascript
使用express来代理服务的方法
2019/06/21 Javascript
java遇到微信小程序 "支付验证签名失败" 问题解决
2019/12/22 Javascript
JS面向对象实现飞机大战
2020/08/26 Javascript
vue开发chrome插件,实现获取界面数据和保存到数据库功能
2020/12/01 Vue.js
python密码错误三次锁定(实例讲解)
2017/11/14 Python
全面分析Python的优点和缺点
2018/02/07 Python
pandas.DataFrame.to_json按行转json的方法
2018/06/05 Python
详解Django 中是否使用时区的区别
2018/06/14 Python
python3使用flask编写注册post接口的方法
2018/12/28 Python
ActiveMQ:使用Python访问ActiveMQ的方法
2019/01/30 Python
python 返回一个列表中第二大的数方法
2019/07/09 Python
python通过函数名调用函数的几种场景
2020/09/23 Python
原生 JS+CSS+HTML 实现时序图的方法
2019/07/31 HTML / CSS
世界上第一个水枕头:Mediflow
2018/12/06 全球购物
大学生自我鉴定
2013/12/08 职场文书
环境建设实施方案
2014/03/14 职场文书
施工安全标语
2014/06/07 职场文书
城市轨道交通工程职业生涯规划书范文
2014/09/16 职场文书
社会体育专业大学生职业生涯规划书
2014/09/17 职场文书
水知道答案观后感
2015/06/08 职场文书