Python生成pdf文件的方法
更新时间:2014年08月04日 18:07:05 投稿:shichen2014
这篇文章主要介绍了Python生成pdf文件的方法,比较实用的功能,需要的朋友可以参考下
本文实例演示了Python生成pdf文件的方法,是比较实用的功能,主要包含2个文件。具体实现方法如下:
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 合并多个格式一致的 Excel 文件(推荐)
这篇文章主要介绍了使用 Python 合并多个格式一致的 Excel 文件,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2019-12-12
ubuntu安装sublime3并配置python3环境的方法
这篇文章主要介绍了ubuntu安装sublime3并配置python3环境的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-03-03


最新评论