这是《python基础教程》中的第二个项目,关于python操作PDF。
涉及到的知识点
1、urllib的使用
2、reportlab库的使用
这个例子着实很简单,不过我发现在python里面可以直接在数组[]里面写for循环,真是越用越方便。
下面是代码:
from urllib import urlopenfrom reportlab.graphics.shapes import *from reportlab.graphics.charts.lineplots import LinePlotfrom reportlab.graphics.charts.textlabels import Labelfrom reportlab.graphics import renderPDFURL = 'http://MENT_CHARS: data.append([float(n) for n in line.split()])pred = [row[2] for row in data]high = [row[3] for row in data]low = [row[4] for row in data]times = [row[0] + row[1]/12.0 for row in data]lp = LinePlot()lp.x = 50lp.y = 50lp.height = 125lp.width = 300lp.data = [zip(times, pred),zip(times,high),zip(times, low)]lp.lines[0].strokeColor = colors.bluelp.lines[1].strokeColor = colors.redlp.lines[2].strokeColor = colors.greendrawing.add(lp)drawing.add(String(250,150, 'Sunspots',fontSize=14,fillColor=colors.red))renderPDF.drawToFile(drawing, 'report3.pdf','Sunspots')以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。