python生成pdf报表_Python学习之使用Python生成PDF报告

在有些时候运维同事需要对一些数据收集后形成PDF报告的形式发送出去。利用python的reportlab库可以帮我们很快的实现自定义生成PDF报告。

在CentOS 下通过sudo yum install python-reportlab -y 安装reportlab库

#/usr/bin/python

from reportlab.pdfgen import canvas

def hello():                           #定义hello函数

c=canvas.Canvas("Helloworld.pdf")       #定义文件名称,会自动创建文件

c.drawString(100,100,"Hello World")     #简单的文件内容布局和内容

c.showPage()                            #停止画图

c.save()                                #创建PDF

hello()

#/usr/bin/python

import subprocess

import datetime

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="disk_report.pdf"):   #创建PDF文件

now=datetime.datetime.today()

date=now.strftime("%h %d %Y %H:%M:%S")

c=canvas.Canvas(output)

textobject=c.beginText()

textobject.setTextOrigin(inch,11*inch)

textobject.textLines('''

Disk Capacity Report: %s

''' % date)

for line in input:

textobject.textLine(line.strip())

c.drawText(textobject)

c.showPage()

c.save()

report=disk_report()

create_pdf(report)

利用reportlab库还可以在PDF中添加颜色和图表。

可以通过查看文档学习如何详细使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值