手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Python 生成pdf文件

[日期:2013-06-24] 来源:Linux社区  作者:ghostid [字体:大 中 小]

分享两个Python程序,以下程序均来自《Python.UNIX和Linux系统管理指南》http://www.linuxidc.com/Linux/2013-06/86448.htm

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)

效果

Python 生成pdf文件

linux
相关资讯       python 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款