python基礎教程項目二之畫幅好畫
更新時間:2018年04月02日 10:21:20 作者:the5fire
這篇文章主要為大家詳細介紹了python基礎教程項目二之畫幅好畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下
這是《python基礎教程》中的第二個項目,關于python操作PDF。
涉及到的知識點
1、urllib的使用
2、reportlab庫的使用
這個例子著實很簡單,不過我發(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')
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
淺析Python如何實現(xiàn)Celery任務隊列系統(tǒng)
這篇文章主要為大家詳細介紹了一個基于 Celery 和 Redis 的分布式任務隊列系統(tǒng),用于處理異步任務和定時任務,希望對大家有一定的幫助2025-04-04
使用icecream實現(xiàn)優(yōu)雅調試Python代碼
在大型項目中,使用print()調試代碼可能導致終端輸出過多,難以分辨輸出結果與代碼的對應關系,為了更清晰地調試,可以采用Icecream庫,本文介紹了如何使用icecream實現(xiàn)優(yōu)雅調試Python代碼,需要的朋友可以參考下2024-08-08
Python導入txt數(shù)據(jù)到mysql的方法
這篇文章主要介紹了Python導入txt數(shù)據(jù)到mysql的方法,涉及Python操作txt文件及mysql數(shù)據(jù)庫的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
如何基于OpenCV&Python實現(xiàn)霍夫變換圓形檢測
最近開始學習opencv,想檢測圖片上的圓環(huán),發(fā)現(xiàn)霍夫變換可以做這樣的效果出來,于是嘗試用霍夫變換做了下圓環(huán)檢測,這篇文章主要給大家介紹了基于OpenCV&Python實現(xiàn)霍夫變換圓形檢測的相關資料,需要的朋友可以參考下2021-08-08

