前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python学习之使用Python生成P

Python学习之使用Python生成P

作者头像
py3study
发布2020-01-06 19:22:31
6950
发布2020-01-06 19:22:31
举报
文章被收录于专栏:python3python3

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

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

代码语言:javascript
复制
#/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()
代码语言:javascript
复制
#/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中添加颜色和图表。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档