python数据生成pdf,Python生成pdf文件的方法

摘要:这篇Python开发技术栏目下的“Python生成pdf文件的方法”,介绍的技术点是“python生成pdf文件、python生成pdf、生成pdf文件、Python、生成PDF、pdf文件”,希望对大家开发技术学习和问题解决有帮助。这篇文章主要介绍了Python生成pdf文件的方法,比较实用的功能,需要的朋友可以参考下

本文实例演示了Python生成pdf文件的方法,是比较实用的功能,主要包含2个文件。具体实现方法如下:

pdf.py文件如下:

#!/usr/bin/python

from reportlab.pdfgen import canvas

def hello():

c = canvas.Canvas("helloworld.pdf")

c.drawString(100,100,"Hello,World")

c.showPage()

c.save()

hello()

diskreport.py文件如下:

#!/usr/bin/env 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)

# print p.stdout.readlines()

return p.stdout.readlines()

def create_pdf(input, output="disk_report.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 Capcity Report: %s''' %date)

for line in input:

textobject.textLine(line.strip())

c.drawText(textobject)

c.showPage()

c.save()

report = disk_report()

create_pdf(report)

感兴趣的读者可以调试运行一下,对不足之处加以改进,以实现功能的最佳应用!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值