-★出爺;3 发表于 2018-8-3 12:04:08

Python学习之使用Python生成PDF报告

#/usr/bin/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)
  
    return p.stdout.readlines()
  

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

  
    for line in input:
  
      textobject.textLine(line.strip())
  
    c.drawText(textobject)
  
    c.showPage()
  
    c.save()
  

  
report=disk_report()
  
create_pdf(report)
页: [1]
查看完整版本: Python学习之使用Python生成PDF报告