https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2&shareId=400000000398149(博主錄制)
http://www.jb51.net/article/53233.htm
本文實例演示了Python生成pdf文件的方法,是比較實用的功能,主要包含2個文件。具體實現方法如下:
pdf.py文件如下:
1
2
3
4
5
6
7
8
|
#!/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文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#!/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)
|
感興趣的讀者可以調試運行一下,對不足之處加以改進,以實現功能的最佳應用!