当前位置: 移动技术网 > IT编程>脚本编程>Python > Python生成pdf文件的方法

Python生成pdf文件的方法

2019年03月28日  | 移动技术网IT编程  | 我要评论

优真崎航,乡愁教案,918wyt

本文实例演示了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)

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网