欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python生成pdf文件的方法

 更新時(shí)間:2014年08月04日 18:07:05   投稿:shichen2014  
這篇文章主要介紹了Python生成pdf文件的方法,比較實(shí)用的功能,需要的朋友可以參考下

本文實(shí)例演示了Python生成pdf文件的方法,是比較實(shí)用的功能,主要包含2個(gè)文件。具體實(shí)現(xiàn)方法如下:

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)

感興趣的讀者可以調(diào)試運(yùn)行一下,對(duì)不足之處加以改進(jìn),以實(shí)現(xiàn)功能的最佳應(yīng)用!

相關(guān)文章

最新評(píng)論