1.安装reportlab库
http://www.reportlab.com/ftp/
ubuntu可以直接 apt-get install python-reportlab
2.实验
>>> from reportlab.pdfgen import canvas >>> def hello(): c = canvas.Canvas("hello World.pdf") //指定pdf目录和文件名 c.drawString(100,100,"helo World") //输出区域及内容 c.showPage() c.save() //保存
综合案例
>>> import datetime,subprocess >>> from reportlab.pdfgen import canvas >>> from reportlab.lib.units import inch >>> >>> def dir_report(): p = subprocess.Popen("dir",shell=True,stdout=subprocess.PIPE) return p.stdout.readlines() >>> def create_pdf(input,output="dir_report.pdf"): now = datetime.datetime.today() date = now.strftime("%h %d %Y %H:%M:%S") c = canvas.Canvas(output) textobj = c.beginText() textobj.setTextOrigin(inch,11*inch) textobj.textLines(''''' This history sub dir and file is time is %s''' % date) for line in input: textobj.textLine(line.strip()) c.drawText(textobj) c.showPage() c.save() >>> report = dir_report() >>> create_pdf()