這是《python基礎(chǔ)教程》中的第二個(gè)項(xiàng)目,關(guān)于python操作PDF。
涉及到的知識(shí)點(diǎn)
1、urllib的使用
2、reportlab庫的使用
這個(gè)例子著實(shí)很簡(jiǎn)單,不過我發(fā)現(xiàn)在python里面可以直接在數(shù)組[]里面寫for循環(huán),真是越用越方便。
下面是代碼:
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')
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com