python xlsxwriter庫生成圖表的應(yīng)用示例
xlsxwriter可能用過的人并不是很多,不過使用后就會感覺,他的功能讓你嘆服,除了可以按要求生成你所需要的excel外
還可以加上很形象的各種圖,比如柱狀圖、餅圖、折線圖等。
xlsxwriter 基本用法,創(chuàng)建 xlsx 文件并添加數(shù)據(jù)
官方文檔:http://xlsxwriter.readthedocs.org/
xlsxwriter 可以操作 xls 格式文件
注意:xlsxwriter 只能創(chuàng)建新文件,不可以修改原有文件。如果創(chuàng)建新文件時與原有文件同名,則會覆蓋原有文件
Linux 下安裝: sudo pip install XlsxWriter
Windows 下安裝: pip install XlsxWriter
請看本人生成的:
這里包含了數(shù)據(jù)公式的計算,插入圖片的連接,生成的圖表,當然如果你還需要其他的功能,可以繼續(xù)參考庫的文檔
下面把源代碼貼出來,希望對大家在工作遇到類似的情況,可以直接拿去用。
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Eric.yue import xlsxwriter import random from datetime import date import collections def xlwt_chart(xl_obj,table): #生成柱狀圖 column_chart = xl_obj.add_chart({'type':'column'}) column_chart.add_series({ 'name':'=sheet1!$D$1', 'categories':'=sheet1!$D$2:$D$7', 'values':'=sheet1!$E$2:$E$7' }) table.insert_chart('G2',column_chart) #生成餅圖 column_chart2 = xl_obj.add_chart({'type':'pie'}) column_chart2.add_series({ 'name': '=sheet1!$D$1', 'categories':'=sheet1!$D$2:$D$7', 'values': '=sheet1!$E$2:$E$7' }) table.insert_chart('G20', column_chart2) def xlwt_run(): data_base = ['0-50','50-60','60-70','70-80','80-90','90-100'] #生成一個有序的字典 chart_dict = collections.OrderedDict.fromkeys(data_base,0) xl_obj = xlsxwriter.Workbook('chart.xlsx') table = xl_obj.add_worksheet('sheet1') table.write_string(0,0,u'姓名') table.write_string(0,1,u'成績') table.write_string(0,2,u'日期') table.merge_range('D1:E1', u'成績分布') table.set_column('C:E',15) #定義格式 date_format = xl_obj.add_format({'num_format':'yyyy-mm-dd'}) color_format = xl_obj.add_format({'color':'red'}) font_format = xl_obj.add_format({'font_color':'green','bold':True}) mm = 1 for i in xrange(1,40): name = 'name_%d' % i score = random.randint(30,100) if score <= 50: chart_dict['0-50'] += 1 elif score>50 and score<=60: chart_dict['50-60'] += 1 elif score>60 and score<=70: chart_dict['60-70'] += 1 elif score>70 and score<=80: chart_dict['70-80'] += 1 elif score>80 and score<=90: chart_dict['80-90'] += 1 else: chart_dict['90-100'] += 1 if score > 60: table.write_string(i, 0, name) table.write_number(i, 1, score) else: table.write_string(i, 0, name, color_format) table.write_number(i, 1, score, color_format) table.write_datetime(i, 2,date.today(), date_format) mm = mm + 1 #生成圖表數(shù)據(jù) row = 1 for k,v in chart_dict.items(): table.write_string(row, 3, k, font_format) table.write_number(row, 4, v, font_format) row = row+1 xlwt_chart(xl_obj,table) #使用公式 table.write_formula(mm,1,'=AVERAGE(B2:B40)') #插入帶鏈接的圖片 table.insert_image('D20',r'/home/mywork/pythonchina/cto51_log/bd_logo12.png',{'url':'https://www.baidu.com'}) #關(guān)閉excel句柄 xl_obj.close() if __name__ == '__main__': xlwt_run()
沒有使用類寫,只是即興而作。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python開發(fā)之Nginx+uWSGI+virtualenv多項目部署教程
這篇文章主要介紹了Python系列之-Nginx+uWSGI+virtualenv多項目部署教程,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05解決django跨域的問題小結(jié)(Hbuilder X)
使用Django開發(fā)時,可能會遇到跨域問題,尤其是當后端與HbuilderX開發(fā)的前端結(jié)合使用時,解決此問題的關(guān)鍵步驟包括安裝django-cors-headers庫,并在Django的settings.py中進行相應(yīng)配置,本文給大家介紹解決django跨域的問題小結(jié),感興趣的朋友一起看看吧2024-10-10Python實現(xiàn)獲取操作系統(tǒng)版本信息方法
這篇文章主要介紹了Python實現(xiàn)獲取操作系統(tǒng)版本信息方法,本文在命令行中獲取操作系統(tǒng)信息,介紹了platform模塊的使用,需要的朋友可以參考下2015-04-04Python中關(guān)于字符串對象的一些基礎(chǔ)知識
這篇文章主要介紹了詳解Python中的字符串對象,關(guān)于字符串的操作和特性是Python學習當中的基礎(chǔ)知識,需要的朋友可以參考下2015-04-04Windows安裝Python、pip、easy_install的方法
這篇文章主要介紹了Windows安裝Python、pip、easy_install的方法,需要的朋友可以參考下2017-03-03Python實現(xiàn)PDF轉(zhuǎn)為Excel的示例講解
這篇文章主要為大家詳細介紹了在Python中將PDF表格轉(zhuǎn)換為Excel文件的解決方案,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學習一下2023-11-11