Pythonがpdfファイルを生成する方法
1406 ワード
この例では,Pythonがpdfファイルを生成する方法を実証し,比較的実用的な機能であり,主に2つのファイルを含む.具体的な実現方法は以下の通りである.
pdf.pyファイルは次のとおりです.
diskreport.pyファイルは次のとおりです.
興味のある読者はデバッグして実行し、不足点を改善して、機能の最適な応用を実現することができます.
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)
興味のある読者はデバッグして実行し、不足点を改善して、機能の最適な応用を実現することができます.