python基礎(chǔ)教程項目二之畫幅好畫
這是《python基礎(chǔ)教程》中的第二個項目,關(guān)于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')
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python基礎(chǔ)教程之?dāng)?shù)字處理(math)模塊詳解
- python基礎(chǔ)教程之popen函數(shù)操作其它程序的輸入和輸出示例
- python基礎(chǔ)教程之類class定義使用方法
- python基礎(chǔ)教程之基本數(shù)據(jù)類型和變量聲明介紹
- python基礎(chǔ)教程之lambda表達(dá)式使用方法
- Python安裝使用命令行交互模塊pexpect的基礎(chǔ)教程
- python基礎(chǔ)教程之實現(xiàn)石頭剪刀布游戲示例
- python基礎(chǔ)教程項目四之新聞聚合
- python基礎(chǔ)教程項目三之萬能的XML
- python基礎(chǔ)教程項目五之虛擬茶話會
相關(guān)文章
淺析Python如何實現(xiàn)Celery任務(wù)隊列系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了一個基于 Celery 和 Redis 的分布式任務(wù)隊列系統(tǒng),用于處理異步任務(wù)和定時任務(wù),希望對大家有一定的幫助2025-04-04
使用icecream實現(xiàn)優(yōu)雅調(diào)試Python代碼
在大型項目中,使用print()調(diào)試代碼可能導(dǎo)致終端輸出過多,難以分辨輸出結(jié)果與代碼的對應(yīng)關(guān)系,為了更清晰地調(diào)試,可以采用Icecream庫,本文介紹了如何使用icecream實現(xiàn)優(yōu)雅調(diào)試Python代碼,需要的朋友可以參考下2024-08-08
Python導(dǎo)入txt數(shù)據(jù)到mysql的方法
這篇文章主要介紹了Python導(dǎo)入txt數(shù)據(jù)到mysql的方法,涉及Python操作txt文件及mysql數(shù)據(jù)庫的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
如何基于OpenCV&Python實現(xiàn)霍夫變換圓形檢測
最近開始學(xué)習(xí)opencv,想檢測圖片上的圓環(huán),發(fā)現(xiàn)霍夫變換可以做這樣的效果出來,于是嘗試用霍夫變換做了下圓環(huán)檢測,這篇文章主要給大家介紹了基于OpenCV&Python實現(xiàn)霍夫變換圓形檢測的相關(guān)資料,需要的朋友可以參考下2021-08-08

