Python 官方文档:入门教程 => 点击学习
在有些时候运维同事需要对一些数据收集后形成pdf报告的形式发送出去。利用python的reportlab库可以帮我们很快的实现自定义生成PDF报告。在Centos 下通过sudo yum install Python-reportlab -
在有些时候运维同事需要对一些数据收集后形成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中添加颜色和图表。
可以通过查看文档学习如何详细使用
Http://www.reportlab.com/docs/reportlab-userguide.pdf
--结束END--
本文标题: Python学习之使用Python生成P
本文链接: https://www.lsjlt.com/news/191524.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0