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设计模式之策略模式
Nov 18 Python
Python实现针对含中文字符串的截取功能示例
Sep 22 Python
Python实现字符串的逆序 C++字符串逆序算法
May 28 Python
关于python写入文件自动换行的问题
Jun 23 Python
浅谈python标准库--functools.partial
Mar 13 Python
Python玩转加密的技巧【推荐】
May 13 Python
Python利用matplotlib做图中图及次坐标轴的实例
Jul 08 Python
python 两个数据库postgresql对比
Oct 21 Python
从numpy数组中取出满足条件的元素示例
Nov 26 Python
python 操作mysql数据中fetchone()和fetchall()方式
May 15 Python
完美解决torch.cuda.is_available()一直返回False的玄学方法
Feb 06 Python
python实现ROA算子边缘检测算法
Apr 05 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实现的多彩标签效果代码分享
2014/08/21 PHP
PHP实现股票趋势图和柱形图
2015/02/07 PHP
详解php语言最牛掰的Laravel框架
2017/11/20 PHP
PHP批斗大会之缺失的异常详解
2019/07/09 PHP
在网页里看flash的trace数据的js类
2009/01/10 Javascript
一个关于javascript匿名函数的问题分析
2012/03/30 Javascript
在表单提交前进行验证的几种方式整理
2013/07/31 Javascript
javascript打开word文档的方法
2014/04/16 Javascript
jQuery实现Meizu魅族官方网站的导航菜单效果
2015/09/14 Javascript
jQuery实现网页顶部固定导航效果代码
2015/12/24 Javascript
js 动态生成html 触发事件传参字符转义的实例
2017/02/14 Javascript
JS简单获取日期相差天数的方法
2017/04/24 Javascript
使用OPENLAYERS3实现点选的方法
2020/09/24 Javascript
JS/HTML5游戏常用算法之碰撞检测 包围盒检测算法详解【凹多边形的分离轴检测算法】
2018/12/13 Javascript
jQuery实现的3D版图片轮播示例【滑动轮播】
2019/01/18 jQuery
vue使用prop可以渲染但是打印台报错的解决方式
2019/11/13 Javascript
ES6扩展运算符和rest运算符用法实例分析
2020/05/23 Javascript
Python判断文件或文件夹是否存在的三种方法
2017/07/27 Python
Python爬虫获取整个站点中的所有外部链接代码示例
2017/12/26 Python
python中使用xlrd读excel使用xlwt写excel的实例代码
2018/01/31 Python
python解决pandas处理缺失值为空字符串的问题
2018/04/08 Python
python循环输出三角形图案的例子
2019/11/22 Python
使用keras2.0 将Merge层改为函数式
2020/05/23 Python
CSS3 完美实现圆角效果
2009/07/13 HTML / CSS
CSS3实现水平居中、垂直居中、水平垂直居中的实例代码
2020/02/27 HTML / CSS
泰国综合购物网站:Lazada泰国
2018/04/09 全球购物
写一个在SQL Server创建表的SQL语句
2012/03/10 面试题
C#实现对任一张表的数据进行增,删,改,查要求,运用Webservice,体现出三层架构
2014/07/11 面试题
说一下Linux下有关用户和组管理的命令
2016/01/04 面试题
大学毕业生通用自荐信范文
2013/10/31 职场文书
信访工作经验交流材料
2014/05/23 职场文书
幼儿园校园小喇叭广播稿
2014/10/17 职场文书
Python字符串对齐方法使用(ljust()、rjust()和center())
2021/04/26 Python
英镑符号 £
2022/02/17 杂记
Golang原生rpc(rpc服务端源码解读)
2022/04/07 Golang
python APScheduler执行定时任务介绍
2022/04/19 Python