python 创建pdf文件并支持中文 reportlab - 千月的python linux 系统管理指南学习笔记(19)
2017-06-17 23:02
871 查看
reportlab 工具的安装
pdf文件几乎成为我们日常工作中使用的最多的文档格式,我们当然希望能够使用Python来直接创建这种格式的各种报告。
在各种 pdf 库中,最重量级的库应该是 ReportLab。这个库有自由版和商业版两种,一般来说自由版足够日常工作使用。我们需要安装 reportlab 库才能使用。如果本机没有 reportlab 库可以使用如下命令来安装。
pip install reportlab
理论上可以直接安装成功,如果报错有可能是你的pip版本过低,用 pip install --upgrade pip 升级一下,或者与数据源的连接过于不稳定,换个时间多试几次(凌晨效果较好)。
如何创建一个 pdf 文件
from reportlab.pdfgen import canvas
我们看第一个例子。简单的创建一个pdf文件。我们编写一个 hw.py 的程序
加上可执行权限后,执行我们得到了一个 helloworld.pdf 的文件。
打开后就是一个空白的文件,最下面有个 hello world 的小字。
看看代码
我们创建一个 canvas 对象,并定义了生成的文件名。
drawString(100,100,"Hello World") 在 x=100, y=100 的位置写下 Hello World。
showPage() 关闭当前页,并开始新的一页。
save() 保存并关闭此 pdf 文档。
如何创建一个 pdf 的磁盘报告
下面我们创建一个工作中用的到的文件。磁盘使用报告。这里有个问题,因为我们是中文的母语,因此我们希望pdf能够支持中文。而默认的 reportlab 是不支持中文显示的。要做如下处理。
apt-get install ttf-wyq-zenhei
下载一个中文字库,最好是 ttf 格式的。比如文泉字体
好了,开始看代码。我标红了需要输出中文要加的代码,如果您是英文系统,就不用加这几行了。
#!/usr/bin/env python
import datetime #导入日期时间库
import subprocess
import reportlab.pdfbase.ttfonts #导入reportlab的注册字体
reportlab.pdfbase.pdfmetrics.registerFont(reportlab.pdfbase.ttfonts.TTFont('song', '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc')) #注册字体
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch #导入单位英寸
def disk_report():
p = subprocess.Popen("df -h ",shell=True,stdout=subprocess.PIPE)
return p.stdout.readlines() #看不懂的前面有讲解,返回行列表。
def create_pdf(input,output="diskfree.pdf"):
now = datetime.datetime.today()
date = now.strftime("%h %d %Y %H:%M:%S") #设定日期格式
c = canvas.Canvas(output)
c.setFont('song',10) #设置字体字号
textobject = c.beginText() #定义开始
textobject.setTextOrigin(inch,11*inch) #定义位置
textobject.textLines('''Disk Capacity Report: %s ''' % date ) #输出标题
for line in input: #通过循环的方式一行一行写入文件
line=line.decode("utf8") #转换line变量为 utf8 格式
textobject.textLine(line.strip()) #写入文件
c.drawText(textobject)
c.showPage()
c.save()
report = disk_report()
create_pdf(report)
执行结果是:
pdf文件几乎成为我们日常工作中使用的最多的文档格式,我们当然希望能够使用Python来直接创建这种格式的各种报告。
在各种 pdf 库中,最重量级的库应该是 ReportLab。这个库有自由版和商业版两种,一般来说自由版足够日常工作使用。我们需要安装 reportlab 库才能使用。如果本机没有 reportlab 库可以使用如下命令来安装。
pip install reportlab
理论上可以直接安装成功,如果报错有可能是你的pip版本过低,用 pip install --upgrade pip 升级一下,或者与数据源的连接过于不稳定,换个时间多试几次(凌晨效果较好)。
如何创建一个 pdf 文件
from reportlab.pdfgen import canvas
我们看第一个例子。简单的创建一个pdf文件。我们编写一个 hw.py 的程序
加上可执行权限后,执行我们得到了一个 helloworld.pdf 的文件。
打开后就是一个空白的文件,最下面有个 hello world 的小字。
看看代码
我们创建一个 canvas 对象,并定义了生成的文件名。
drawString(100,100,"Hello World") 在 x=100, y=100 的位置写下 Hello World。
showPage() 关闭当前页,并开始新的一页。
save() 保存并关闭此 pdf 文档。
如何创建一个 pdf 的磁盘报告
下面我们创建一个工作中用的到的文件。磁盘使用报告。这里有个问题,因为我们是中文的母语,因此我们希望pdf能够支持中文。而默认的 reportlab 是不支持中文显示的。要做如下处理。
apt-get install ttf-wyq-zenhei
下载一个中文字库,最好是 ttf 格式的。比如文泉字体
好了,开始看代码。我标红了需要输出中文要加的代码,如果您是英文系统,就不用加这几行了。
#!/usr/bin/env python
import datetime #导入日期时间库
import subprocess
import reportlab.pdfbase.ttfonts #导入reportlab的注册字体
reportlab.pdfbase.pdfmetrics.registerFont(reportlab.pdfbase.ttfonts.TTFont('song', '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc')) #注册字体
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch #导入单位英寸
def disk_report():
p = subprocess.Popen("df -h ",shell=True,stdout=subprocess.PIPE)
return p.stdout.readlines() #看不懂的前面有讲解,返回行列表。
def create_pdf(input,output="diskfree.pdf"):
now = datetime.datetime.today()
date = now.strftime("%h %d %Y %H:%M:%S") #设定日期格式
c = canvas.Canvas(output)
c.setFont('song',10) #设置字体字号
textobject = c.beginText() #定义开始
textobject.setTextOrigin(inch,11*inch) #定义位置
textobject.textLines('''Disk Capacity Report: %s ''' % date ) #输出标题
for line in input: #通过循环的方式一行一行写入文件
line=line.decode("utf8") #转换line变量为 utf8 格式
textobject.textLine(line.strip()) #写入文件
c.drawText(textobject)
c.showPage()
c.save()
report = disk_report()
create_pdf(report)
执行结果是:
相关文章推荐
- Python下字符串的创建和转义字符的使用 - 千月的python linux 系统管理指南学习笔记(10)
- Python 创建、读取和写入文件以及yield关键字- 千月的python linux 系统管理指南学习笔记(14)
- python socket 的使用 - 千月的python linux 系统管理指南学习笔记(20)
- python httplib urllib的使用 - 千月的python linux 系统管理指南学习笔记(21)
- IPython下目录的操作-魔力函数(cd pwd bookmark dhist )- 千月的python linux 系统管理指南学习笔记(7)
- IPython(jupyter)简单介绍和基本交互操作 - 千月的python linux 系统管理指南学习笔记(5)
- IPython 下的信息收集-魔力函数(page pinfo who等)- 千月的python linux 系统管理指南学习笔记(9)
- IPython下执行shell命令-魔力函数(alias store rehash)- 千月的python linux 系统管理指南学习笔记(6)
- python 制作图表 matplotlib - 千月的python linux 系统管理指南学习笔记(18)
- python 对异常的处理(try...finally...和with 方法)- 千月的python linux 系统管理指南学习笔记(15)
- Python 标准输入输出 StringIO urllib- 千月的python linux 系统管理指南学习笔记(16)
- IPython下字符串的简单处理 - 千月的python linux 系统管理指南学习笔记(8)
- python XML_RPC 的使用 - 千月的python linux 系统管理指南学习笔记(22)
- Python中使用函数 - 千月的python linux 系统管理指南学习笔记(4)
- 一些关于Python的基础知识 - 千月的python linux 系统管理指南学习笔记(3)
- Linux学习笔记-与系统管理有关的命令[转帖]
- Linux 学习笔记 -- 第四部分 Linux 使用着管理 -- 第15章 磁盘配额 (Quota) 与高级文件系统管理
- Linux磁盘与文件系统管理学习笔记
- Linux 学习笔记_9_文件系统管理_3_/etc/fstab文件分析与磁盘配额应用
- Linux学习笔记3——Linux中常用系统管理命令