python xlsxwriter庫組建圖表的應用

來源:互聯網
上載者:User

標籤:alt   除了   日期   rand   直接   www   bin   order   bsp   

xlsxwriter可能用過的人並不是很多,不過使用後就會感覺,他的功能讓你歎服,除了可以按要求產生你所需要的excel外

還可以加上很形象的各種圖,比如柱狀圖、餅圖、折線圖等。

請看本人產生的:

這裡包含了資料公式的計算,插入圖片的串連,產生的圖表,當然如果你還需要其他的功能,可以繼續參考庫的文檔

下面把原始碼貼出來,希望對大家在工作遇到類似的情況,可以直接拿去用。不過轉載的時候請註明出處。

 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author:Eric.yue 4  5 import xlsxwriter 6 import random 7 from datetime import date 8 import collections 9 10 def xlwt_chart(xl_obj,table):11 12     #產生柱狀圖13     column_chart = xl_obj.add_chart({‘type‘:‘column‘})14     column_chart.add_series({15         ‘name‘:‘=sheet1!$D$1‘,16         ‘categories‘:‘=sheet1!$D$2:$D$7‘,17         ‘values‘:‘=sheet1!$E$2:$E$7‘18     })19     table.insert_chart(‘G2‘,column_chart)20 21     #產生餅圖22     column_chart2  = xl_obj.add_chart({‘type‘:‘pie‘})23     column_chart2.add_series({24         ‘name‘: ‘=sheet1!$D$1‘,25         ‘categories‘:‘=sheet1!$D$2:$D$7‘,26         ‘values‘: ‘=sheet1!$E$2:$E$7‘27     })28     table.insert_chart(‘G20‘, column_chart2)29 30 31 def xlwt_run():32     data_base = [‘0-50‘,‘50-60‘,‘60-70‘,‘70-80‘,‘80-90‘,‘90-100‘]33 34     #產生一個有序的字典35     chart_dict = collections.OrderedDict.fromkeys(data_base,0)36 37     xl_obj = xlsxwriter.Workbook(‘chart.xlsx‘)38     table = xl_obj.add_worksheet(‘sheet1‘)39     table.write_string(0,0,u‘姓名‘)40     table.write_string(0,1,u‘成績‘)41     table.write_string(0,2,u‘日期‘)42     table.merge_range(‘D1:E1‘, u‘成績分布‘)43     table.set_column(‘C:E‘,15)44 45     #定義格式46     date_format = xl_obj.add_format({‘num_format‘:‘yyyy-mm-dd‘})47     color_format = xl_obj.add_format({‘color‘:‘red‘})48     font_format = xl_obj.add_format({‘font_color‘:‘green‘,‘bold‘:True})49 50     mm = 151     for i in xrange(1,40):52         name = ‘name_%d‘ % i53         score = random.randint(30,100)54         if score <= 50:55             chart_dict[‘0-50‘] += 156         elif score>50 and score<=60:57             chart_dict[‘50-60‘] += 158         elif score>60 and score<=70:59             chart_dict[‘60-70‘] += 160         elif score>70 and score<=80:61             chart_dict[‘70-80‘] += 162         elif score>80 and score<=90:63             chart_dict[‘80-90‘] += 164         else:65             chart_dict[‘90-100‘] += 166 67         if score > 60:68             table.write_string(i, 0, name)69             table.write_number(i, 1, score)70         else:71             table.write_string(i, 0, name, color_format)72             table.write_number(i, 1, score, color_format)73 74         table.write_datetime(i, 2,date.today(), date_format)75         mm = mm + 176 77     #組建圖表資料78     row = 179     for k,v in chart_dict.items():80         table.write_string(row, 3, k, font_format)81         table.write_number(row, 4, v, font_format)82         row = row+183 84     xlwt_chart(xl_obj,table)85     #使用公式86     table.write_formula(mm,1,‘=AVERAGE(B2:B40)‘)87     #插入帶連結的圖片88     table.insert_image(‘D20‘,r‘/home/mywork/pythonchina/cto51_log/bd_logo12.png‘,{‘url‘:‘https://www.baidu.com‘})89 90     #關閉excel控制代碼91     xl_obj.close()92 93 if __name__ == ‘__main__‘:94     xlwt_run()

沒有使用類寫,只是即興而作。

 

python xlsxwriter庫組建圖表的應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.