上一篇文章:設計具備JinJa2模板的漁產品交易Word和Python簡易程式
模版:
程式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from docxtpl import DocxTemplate import requests import json r = requests.get('https://data.moa.gov.tw/Service/OpenData/FromM/AquaticTransData.aspx') text = json.loads(r.text) table=[] for row in text: fishtran={} fishtran['date']=row['交易日期'] fishtran['id']=row['品種代碼'] fishtran['fish']=row['魚貨名稱'] fishtran['market']=row['市場名稱'] fishtran['upper']=row['上價'] fishtran['middle']=row['中價'] fishtran['lower']=row['下價'] fishtran['volume']=row['交易量'] fishtran['average']=row['平均價'] table+=[fishtran] context = {'table':table} tpl = DocxTemplate("AquaticTransTemplate.docx") tpl.render(context) tpl.save("AquaticTransPapers.docx") |
執行結果:
沒有留言:
張貼留言