这篇文章主要为大家详细介绍了python基础教程项目二之画幅好画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这是《python基础教程》中的第二个项目,关于python操作pdf。
涉及到的知识点
1、urllib的使用
2、reportlab库的使用
这个例子着实很简单,不过我发现在python里面可以直接在数组[]里面写for循环,真是越用越方便。
下面是代码:
from urllib import urlopen
from reportlab.graphics.shapes import *
from reportlab.graphics.charts.lineplots import lineplot
from reportlab.graphics.charts.textlabels import label
from reportlab.graphics import renderpdf
url = 'http://www.swpc.noaa.gov/ftpdir/weekly/predict.txt'
comment_chars = '#:'
drawing = drawing(400, 200)
data = []
for line in urlopen(url).readlines():
if not line.isspace() and not line[0] in comment_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 = 50
lp.y = 50
lp.height = 125
lp.width = 300
lp.data = [zip(times, pred),zip(times,high),zip(times, low)]
lp.lines[0].strokecolor = colors.blue
lp.lines[1].strokecolor = colors.red
lp.lines[2].strokecolor = colors.green
drawing.add(lp)
drawing.add(string(250,150, 'sunspots',fontsize=14,fillcolor=colors.red))
renderpdf.drawtofile(drawing, 'report3.pdf','sunspots')
相关推荐:
python基础教程项目四之新闻聚合
以上就是python基础教程项目二之画幅好画的详细内容。