Python生成pdf文件 Python生成pdf文件的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python生成pdf文件 Python生成pdf文件的方法

  2021-03-19 我要评论
想了解Python生成pdf文件的方法的相关内容吗,在本文为您仔细讲解Python生成pdf文件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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)

感兴趣的读者可以调试运行一下,对不足之处加以改进,以实现功能的最佳应用!

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们