Python生成pdf文件的方法
作者:shichen2014 时间:2021-05-11 13:05:36
本文实例演示了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,生成,pdf
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用Python脚本实现自动刷网课
2023-04-20 12:39:16
![](https://img.aspxhome.com/file/2023/1/68901_0s.jpg)
人工智能学习Pytorch张量数据类型示例详解
2021-09-13 01:33:08
![](https://img.aspxhome.com/file/2023/5/72795_0s.png)
Bootstrap3制作自己的导航栏
2023-08-23 02:13:08
![](https://img.aspxhome.com/file/2023/2/56082_0s.png)
php写入mysql中文乱码的实例解决方法
2023-10-10 01:36:49
numpy系列之数组重塑的实现
2022-10-20 13:36:35
清除代码异味
2011-05-21 08:34:00
SQL存储过程介绍
2008-02-13 18:52:00
网站设计应当让用户选,别让用户想
2008-03-19 12:01:00
浅析PEP572: 海象运算符
2023-06-28 10:56:10
Python抽象类的新写法
2022-12-04 13:39:38
pytorch之torchvision.transforms图像变换实例
2021-05-19 05:44:05
请谨慎对待程序的图标和名称
2011-06-16 20:35:22
详解go语言json的使用技巧
2023-09-12 10:27:48
Python数据分析之 Matplotlib 3D图详情
2021-03-05 21:20:33
![](https://img.aspxhome.com/file/2023/6/69466_0s.webp)
Python正则表达式的另类解答
2023-08-02 06:58:04
![](https://img.aspxhome.com/file/2023/7/61717_0s.png)
详解Python如何实现发送带附件的电子邮件
2022-03-20 13:33:46
![](https://img.aspxhome.com/file/2023/2/64742_0s.png)
python实现马耳可夫链算法实例分析
2022-08-13 00:27:08
PHP开发技巧之PHAR反序列化详解
2023-11-15 02:23:45
![](https://img.aspxhome.com/file/2023/3/55633_0s.png)
python 协程 gevent原理与用法分析
2021-10-12 23:36:19
ASPJPEG组件简要攻略之水印、缩略图和描边代码
2008-12-17 12:08:00