python reportlab: pdfgen(二)

  • 颜色
    • RGB颜色
    • RGB颜色透明度
    • CMYK颜色
  • 颜色overPrint
    • 使用白色形成在色块上抠字的效果
  • 标准字体和文本对象
  • 文本对象工具
    • 字符间距
    • 单词间距
    • 水平比例
    • 行距
    • 上下标

颜色

PDF中常用的颜色有两个类型:

  1. RGB:常用于屏幕显示
  2. CMYK:常用于专业的印刷场景

RGB颜色

RGB即红(red),绿(green)和蓝(blue)。用这三种颜色的参数形成各种不同的颜色,比如 (1,1,1) 就代表白色。
在pdfgen中有3中方法可以生成颜色:

  1. 使用color模块
  2. 使用RGB值
  3. 使用灰度
    下面的例子展示了这些方法:
from reportlab.pdfgen import canvasdef colorsRGB(canvas):from reportlab.lib import colorsfrom reportlab.lib.units import inch# 使用color模块生成颜色black = colors.blacky = x = 0; dy=inch*3/4.0; dx=inch*5.5/5; w=h=dy/2; rdx=(dx-w)/2rdy=h/5.0; texty=h+2*rdycanvas.setFont("Helvetica",10)for [namedcolor, name] in ([colors.lavenderblush, "lavenderblush"],[colors.lawngreen, "lawngreen"],[colors.lemonchiffon, "lemonchiffon"],[colors.lightblue, "lightblue"],[colors.lightcoral, "lightcoral"]):canvas.setFillColor(namedcolor)canvas.rect(x+rdx, y+rdy, w, h, fill=1)canvas.setFillColor(black)canvas.drawCentredString(x+dx/2, y+texty, name)x = x+dxy = y + dy; x = 0# 使用RGB值生成颜色for rgb in [(1,0,0), (0,1,0), (0,0,1), (0.5,0.3,0.1), (0.4,0.5,0.3)]:r,g,b = rgbcanvas.setFillColorRGB(r,g,b)canvas.rect(x+rdx, y+rdy, w, h, fill=1)canvas.setFillColor(black)canvas.drawCentredString(x+dx/2, y+texty, "r%s g%s b%s"%rgb)x = x+dxy = y + dy; x = 0# 使用灰度生成颜色for gray in (0.0, 0.25, 0.50, 0.75, 1.0):canvas.setFillGray(gray)canvas.rect(x+rdx, y+rdy, w, h, fill=1)canvas.setFillColor(black)canvas.drawCentredString(x+dx/2, y+texty, "gray: %s"%gray)x = x+dxc= canvas.Canvas("colorsRGB.pdf")
colorsRGB(c)
c.showPage()
c.save()

效果如下:

RGB颜色透明度

在pdfgen中有两种方式设置颜色透明度:

  1. 在CMYK中可以使用overPrint,这在之后会详细介绍。
  2. 在RGB中可以通过调节α参数来设置透明度,默认的α值是1,即完全不透明,其取值范围为[0,1]。
    两种方法的效果类似。下面是调节α参数的例子:
from reportlab.pdfgen import canvasdef alpha(canvas):from reportlab.lib.colors import Color, black, blue, red#透明度为50%的红色red50transparent = Color( 100, 0, 0, alpha=0.5)c = canvas c.setFillColor(black)c.setFont('Helvetica', 10)c.drawString(25,180, 'solid')c.setFillColor(blue)c.rect(25,25,100,100, fill=True, stroke=False)c.setFillColor(red)c.rect(100,75,100,100, fill=True, stroke=False)c.setFillColor(black)c.drawString(225,180, 'transparent')c.setFillColor(blue)c.rect(225,25,100,100, fill=True, stroke=False)c.setFillColor(red50transparent)c.rect(300,75,100,100, fill=True, stroke=False)c= canvas.Canvas("alpha.pdf")
alpha(c)
c.showPage()
c.save()

效果如下图:

CMYK颜色

CMYK(印刷四色模式)是四种标准颜色混合叠加形成各种印刷颜色的模式。四种标准颜色是:C:Cyan = 青色,又称为‘天蓝色’或是‘湛蓝’;M:Magenta = 品红色,又称为‘洋红色’;Y:Yellow = 黄色;K:blacK=黑色。生成CMYK中各种颜色的值可以用[0,1](真值)表示,也可以用[0,100](整值)表示。0表示没有颜料,即白色;1或100表示某种颜色的最大值。下面是使用CMYK颜色的例子:

from reportlab.pdfgen import canvasdef colorsCMYK(canvas):from reportlab.lib.colors import CMYKColorfrom reportlab.lib.units import inch# 使用CMYK真值生成黑色black = CMYKColor(0,0,0,1)# 使用CMYK整值生成青色# cyan = PCMYKColor(100,0,0,0)y = x = 0; dy=inch*3/4.0; dx=inch*5.5/5; w=h=dy/2; rdx=(dx-w)/2rdy=h/5.0; texty=h+2*rdycanvas.setFont("Helvetica",10)y = y + dy; x = 0# 分别生成青色、品红色、黄色、黑色和白色for cmyk in [(1,0,0,0), (0,1,0,0), (0,0,1,0), (0,0,0,1), (0,0,0,0)]:c,m,y1,k = cmykcanvas.setFillColorCMYK(c,m,y1,k)canvas.rect(x+rdx, y+rdy, w, h, fill=1)canvas.setFillColor(black)canvas.drawCentredString(x+dx/2, y+texty, "c%s m%s y%s k%s"%cmyk)x = x+dxc= canvas.Canvas("colorsCMYK.pdf")
colorsCMYK(c)
c.showPage()
c.save()

效果如下:

颜色overPrint

在CMYK中如果两种颜色重叠,就不能像RGB中那样使用α值来调节透明度了,只能使用overPrint,使重叠的部分的颜色混合。注意overprint的效果要用Adobe Acrobat Reader才能看得出来,其他pdf软件看不出混合效果。

使用白色形成在色块上抠字的效果

from reportlab.pdfgen import canvasdef spumoni(canvas):from reportlab.lib.units import inchfrom reportlab.lib.colors import pink, green, brown, whitex = 0; dx = 0.4*inch# 生成底层色块for i in range(4):for color in (pink, green, brown):canvas.setFillColor(color)canvas.rect(x,0,dx,3*inch,stroke=0,fill=1)x = x+dx# 在色块上绘制白色的文字canvas.setFillColor(white)canvas.setStrokeColor(white)canvas.setFont("Helvetica-Bold", 85)canvas.drawCentredString(2.75*inch, 1.3*inch, "SPUMONI")c= canvas.Canvas("spumoni.pdf")
spumoni(c)
c.showPage()
c.save()

效果如下图:

最后一个字母没有显示,是因为它已经在色块外面,和白色的页面融为一体了。
这个例子主要说明,在绘制有颜色的对象的时候要注意图层的先后顺序,后绘制的会覆盖先绘制的。

标准字体和文本对象

setFont函数可以设置字体和字号,setFillColor函数可以为文字添加颜色。例子如下:

from reportlab.pdfgen import canvasdef textsize(canvas):from reportlab.lib.units import inchfrom reportlab.lib.colors import magenta, red# 设置字体和字号canvas.setFont("Times-Roman", 20)# 设置颜色canvas.setFillColor(red)canvas.drawCentredString(2.75*inch, 2.5*inch, "Font size examples")canvas.setFillColor(magenta)size = 7y = 2.3*inchx = 1.3*inchlyrics = ['well she hit Net Solutions','and she registered her own .com site now','and filled it up with yahoo profile pics','she snarged in one night now','and she made 50 million when HUgh Hefner','boutght up the rights now','and she\'ll have fun fun fun','til her Daddy takes the keyboard away']for line in lyrics:canvas.setFont("Helvetica", size)canvas.drawRightString(x,y,"%s points: " % size)canvas.drawString(x,y, line)y = y-size*1.2size = size+1.5c= canvas.Canvas("textsize.pdf")
textsize(c)
c.showPage()
c.save()

效果如下:

下面这个例子展现不同的字体效果:

from reportlab.pdfgen import canvasdef fonts(canvas):from reportlab.lib.units import inchtext = "Now is the time for all good men to..."x = 1.8*inchy = 2.7*inch# 获取所有内置字体for font in canvas.getAvailableFonts():canvas.setFont(font, 10)canvas.drawString(x,y,text)canvas.setFont("Helvetica", 10)canvas.drawRightString(x-10,y, font+":")y = y-13c= canvas.Canvas("fonts.pdf")
fonts(c)
c.showPage()
c.save()

运行结果如下:

其中Symbol和ZapfDingbats因为不能显示正常的文字,所以在这里显示不出来,它们只能显示特定的符号。

文本对象工具

使用drawString生成文本时最快的,除此之外还有很多设置文本对象的工具。pdfgen中的文本对象都包括一个游标,代表文本绘制或更改的位置,这个游标可以在文本行中移动。
**注意:**每次绘制文本,都必须以canvas.beginText()开始,并以canvas.drawText()结束。
下面举一个绘制文本的例子:

from reportlab.pdfgen import canvasdef cursormoves2(canvas):from reportlab.lib.units import inch# 开始绘制文本指令textobject = canvas.beginText()# 设置游标起始位置textobject.setTextOrigin(2, 2.5*inch)textobject.setFont("Helvetica-Oblique", 14)lyrics = ['well she hit Net Solutions','and she registered her own .com site now','and filled it up with yahoo profile pics','she snarged in one night now','and she made 50 million when HUgh Hefner','boutght up the rights now','and she\'ll have fun fun fun','til her Daddy takes the keyboard away']for line in lyrics:# 绘制单行文本但不换行textobject.textOut(line)# 移动游标,注意y轴的正数代表下移点数textobject.moveCursor(14,14) textobject.setFillColorRGB(0.4,0,1)# 绘制多行文本textobject.textLines('''With many apologies to the Beach Boysand anyone else who finds this objectionable''')# 结束绘制文本指令canvas.drawText(textobject)c= canvas.Canvas("cursormoves2.pdf")
cursormoves2(c)
c.showPage()
c.save()

运行效果如下:

循环中,游标每次下移一个字号单位,并且右移一个字号单位。
pdfgen中的文本对象工具总结如下:

# 设置游标起始位置
textobject.setTextOrigin(x,y)
textobject.setTextTransform(a,b,c,d,e,f)
# 移动游标
textobject.moveCursor(dx, dy)
(x,y) = textobject.getCursor()
x = textobject.getX(); y = textobject.getY()
textobject.setFont(psfontname, size, leading = None)
# 绘制单行文本,但不换行
textobject.textOut(text)
# 绘制单行文本,并且换行
textobject.textLine(text='')
# 绘制多行文本
textobject.textLines(stuff, trim=1)

注意:textOuttextLine的区别。使用textLine的时候不需要移动游标来实现换行。

字符间距

setCharSpace可以调整单个字符间的间距。且看下例:

from reportlab.pdfgen import canvasdef charspace(canvas):from reportlab.lib.units import inchtextobject = canvas.beginText()textobject.setTextOrigin(3, 2.5*inch)textobject.setFont("Helvetica-Oblique", 10)# 设置字距大小charspace = 0lyrics = ['well she hit Net Solutions','and she registered her own .com site now','and filled it up with yahoo profile pics','she snarged in one night now','and she made 50 million when HUgh Hefner','boutght up the rights now','and she\'ll have fun fun fun','til her Daddy takes the keyboard away']for line in lyrics:# 应用间距textobject.setCharSpace(charspace)textobject.textLine("%s: %s" %(charspace,line))# 该表间距charspace = charspace+0.5textobject.setFillGray(0.4)textobject.textLines('''With many apologies to the Beach Boysand anyone else who finds this objectionable''')canvas.drawText(textobject)c= canvas.Canvas("charspace.pdf")
charspace(c)
c.showPage()
c.save()

效果如下:

单词间距

setWordSpace可以调整单词间的间距。且看下例:

from reportlab.pdfgen import canvasdef wordspace(canvas):from reportlab.lib.units import inchtextobject = canvas.beginText()textobject.setTextOrigin(3, 2.5*inch)textobject.setFont("Helvetica-Oblique", 12)# 设置词距大小wordspace = 0lyrics = ['well she hit Net Solutions','and she registered her own .com site now','and filled it up with yahoo profile pics','she snarged in one night now','and she made 50 million when HUgh Hefner','boutght up the rights now','and she\'ll have fun fun fun','til her Daddy takes the keyboard away']for line in lyrics:# 应用词距textobject.setWordSpace(wordspace)textobject.textLine("%s: %s" %(wordspace,line))# 改变词距wordspace = wordspace+2.5textobject.setFillColorCMYK(0.4,0,0.4,0.2)textobject.textLines('''With many apologies to the Beach Boysand anyone else who finds this objectionable''')canvas.drawText(textobject)c= canvas.Canvas("wordspace.pdf")
wordspace(c)
c.showPage()
c.save()

效果如下:

水平比例

setHorizScale可以调整文本行的伸缩比例。且看下例:

from reportlab.pdfgen import canvasdef horizontalscale(canvas):from reportlab.lib.units import inchtextobject = canvas.beginText()textobject.setTextOrigin(3, 2.5*inch)textobject.setFont("Helvetica-Oblique", 12)# 设置伸缩比例,默认值是100horizontalscale = 80lyrics = ['well she hit Net Solutions','and she registered her own .com site now','and filled it up with yahoo profile pics','she snarged in one night now','and she made 50 million when HUgh Hefner','boutght up the rights now','and she\'ll have fun fun fun','til her Daddy takes the keyboard away']for line in lyrics:# 应用伸缩比例textobject.setHorizScale(horizontalscale)textobject.textLine("%s: %s" %(horizontalscale,line))# 改变伸缩比例horizontalscale = horizontalscale+10textobject.setFillColorCMYK(0.0,0.4,0.4,0.2)textobject.textLines('''With many apologies to the Beach Boysand anyone else who finds this objectionable   ''')canvas.drawText(textobject)c= canvas.Canvas("horizontalscale.pdf")
horizontalscale(c)
c.showPage()
c.save()

效果如下:

行距

setLeading可以调整文本行距。且看下例:

from reportlab.pdfgen import canvasdef leading(canvas):from reportlab.lib.units import inchtextobject = canvas.beginText()textobject.setTextOrigin(3, 2.5*inch)textobject.setFont("Helvetica-Oblique", 14)# 设置行距大小,单位为点leading = 8lyrics = ['well she hit Net Solutions','and she registered her own .com site now','and filled it up with yahoo profile pics','she snarged in one night now','and she made 50 million when HUgh Hefner','boutght up the rights now','and she\'ll have fun fun fun','til her Daddy takes the keyboard away']for line in lyrics:# 应用行距textobject.setLeading(leading)textobject.textLine("%s: %s" %(leading,line))# 改变行距leading = leading+2.5textobject.setFillColorCMYK(0.8,0,0,0.3)textobject.textLines('''With many apologies to the Beach Boysand anyone else who finds this objectionable''')canvas.drawText(textobject)c= canvas.Canvas("leading.pdf")
leading(c)
c.showPage()
c.save()

效果如下:

上下标

setRise可以将文本设置为上下标。

python reportlab相关推荐

  1. Python Reportlab表 换行

    解决Reportlab表格中的换行问题 str(string).replace('\n','<br />\n') 在字符串中添加 <br / > 标签,即可实现换行. 方法仅供 ...

  2. python数据转换成pdf_python使用reportlab转换jpg为pdf

    今天学习Django时,无意看到了pdf的报表类库Reportlab,使用该库可以很容易的生成pdf.其中介绍最多的是将图片转换为pdf,网上也有相关的介绍(Python Reportlab转换jpg ...

  3. python生成pdf报表

    pyfpdf pyfpdf 基于php的fpdf(http://www.fpdf.org/)移植, 对于字库uincode需要额外安装, 缺少模板, 文档 https://pyfpdf.readthe ...

  4. HelloWorld的几种百变形态,程序员真会玩

    点击上方的终端研发部,右上角选择"设为星标" 生活中几种不一样的helloWorhd 以上图片来自于百度 程序员版的helloWord 实现Hello, world! 的方式: 先 ...

  5. python生成pdf报表_用python的reportlab库生成PDF报表

    前言 reportlab不是python的标准库,它的强大之处在于能满足绝大部分报表的需求形式,这篇文章将介绍reportlab中基本常用的api,使用canvas画出一份整洁的PDF报表.内容均来自 ...

  6. 【Python办公自动化】使用reportlab制作pdf报告

    原文作者:我辈李想 版权声明:文章原创,转载时请务必加上原文超链接.作者信息和本声明. Python使用folium制作地图并生成png图片 第一章 folium的方法和类的介绍(思维导图) 第二章 ...

  7. python打印电子标签--ghostscript 和reportlab实现

    最近公司有一个项目,在仓库中对瓜果蔬菜按照规格打包,打包以后需要称重贴上标签.标签上有条形码,商品编码,商品名称,重量,操作员姓名等信息,这些信息存储在后台数据库中.这本来是一个cs架构的应用,在桌面 ...

  8. python生成图文并茂的pdf--财务报表(一)--reportlab库简介解决安装出错问题

    鄙人在金融公司,领导给了个任务让我用python生产FOF财务报表.觉得帆软生成的不合意,倾向用python,UI给了个设计,让我先行研究开发. 网上的资料不是很多,但是了解到是用到reportlab ...

  9. Python使用Reportlab处理PDF数据 - 图形和图表

    简述 reportlab.graphics子程序包是作为一组独立程序开始的. 今天,它已完全集成到ReportLab工具包的其余部分中. 图形子软件包为开发人员提供了一组强大的功能,可用于创建图表和图 ...

  10. python的reportlab用法

    我们有时候需要用python生成pdf文档,其中有一种办法是使用reportlab,那么具体怎么用呢?下面详细讲述. 使用reportlab设置不同列数的表格宽度. 参考: https://www.i ...

最新文章

  1. shell脚本之日志拆分和监听
  2. PIC中实现printf函数出现:Warning [2066] type qualifier mismatch in assignment
  3. JavaScript实现radianToDegree弧度到度算法(附完整源码)
  4. HDU - 5381 The sum of gcd(莫队/线段树区间合并)
  5. 禾川触摸屏编程软件_汇川PLC编程PLC代写程序
  6. java 反射类成员_java 反射(二)类成员
  7. C#中怎么判断一个数组中是否存在某个数组值
  8. 值类型和引用类型的区别?
  9. python程序流程控制结构_Python程序控制结构 | 分支结构
  10. DEVC++中的 “万能头文件” <bits/stdc++.h>
  11. 信息论与编码_4G与5G分别采用什么信道编码技术_卷积码_Turbo码_LDPC码_Polar码
  12. 4_unittest测试框架_管理测试用例生成测试报告
  13. ios icon 自动生成
  14. android设计稿做ui px,苦逼APPUI设计师的标注切图的利器—PxCook
  15. 通用ESP8266连接阿里云物联网平台
  16. SpringBoot+Mybatis+Mysql结合微信小程序实现登录
  17. 前端优化 | DOSM Web项目优化分析 ( 附解决方案及代码)
  18. RPG手机游戏道具、物品、装备表设计
  19. docker部署开发环境
  20. 【32位系统与64位系统可访问内存的大小】

热门文章

  1. 【谷粒商城】全网最全笔记(1/4)
  2. rs232接口_串口、COM口、UART口,TTL、RS-232、RS-485这么多接口,怎么区分
  3. 一文搞懂激活函数(Sigmoid/ReLU/LeakyReLU/PReLU/ELU)
  4. subli快速度创建html,JS插件——自定义下拉框
  5. linux 蓝牙串口 调试,linux 蓝牙串口 连接android手机调试
  6. 统计学 贾俊平 笔记
  7. leetcode第1282题
  8. 腾讯大讲堂 微信红包系统设计 优化
  9. Mysql 分组求和
  10. Jmeter脚本录制