首页 / 操作系统 / Linux / Python 生成pdf文件
分享两个python程序,以下程序均来自《Python.UNIX和Linux系统管理指南》http://www.linuxidc.com/Linux/2013-06/86448.htmpdf.py#!/usr/bin/pythonfrom reportlab.pdfgen import canvasdef hello(): c = canvas.Canvas("helloworld.pdf") c.drawString(100,100,"Hello,World") c.showPage() c.save()hello()diskreport.py#!/usr/bin/env pythonimport subprocessimport datetimefrom reportlab.pdfgen import canvasfrom reportlab.lib.units import inchdef 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)效果