Application of Python Xlsxwriter Library generation chart

Source: Internet
Author: User

Xlsxwriter may have used a lot of people, but after use will feel, his function to make you admire, in addition to the requirements can be generated by the Excel you need

You can also add a lot of images, such as column chart, pie chart, line chart and so on.

Please see what I have generated:

This includes the calculation of the data formula, the connection of the inserted picture, the generated chart, of course, if you need other functions, you can continue to refer to the library's documentation

Below the source code, I hope that everyone in the work encountered a similar situation, you can directly take to use. However, please specify the source when reproduced.

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #Author:Eric.yue4 5 ImportXlsxwriter6 ImportRandom7  fromDatetimeImportDate8 ImportCollections9 Ten defXlwt_chart (xl_obj,table): One  A     #Create a bar chart -Column_chart = Xl_obj.add_chart ({'type':'column'}) - column_chart.add_series ({ the         'name':'=sheet1! $D $', -         'Categories':'=sheet1! $D $ $D $7', -         'Values':'=sheet1! $E $ $E $7' -     }) +Table.insert_chart ('G2', Column_chart) -  +     #Generate pie chart AColumn_chart2 = Xl_obj.add_chart ({'type':'Pie'}) at column_chart2.add_series ({ -         'name':'=sheet1! $D $', -         'Categories':'=sheet1! $D $ $D $7', -         'Values':'=sheet1! $E $ $E $7' -     }) -Table.insert_chart ('G20', Column_chart2) in  -  to defXlwt_run (): +Data_base = ['0-50','50-60','60-70','70-80','80-90','90-100'] -  the     #generate an ordered dictionary *Chart_dict =collections. Ordereddict.fromkeys (data_base,0) $ Panax NotoginsengXl_obj = Xlsxwriter. Workbook ('chart.xlsx') -Table = Xl_obj.add_worksheet ('Sheet1') theTable.write_string (0,0,u'name') +Table.write_string (0,1,u'Achievements') ATable.write_string (0,2,u'Date') theTable.merge_range ('d1:e1', u'Results Distribution') +Table.set_column ('c:e', 15) -  $     #Defining Formats $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}) the  -MM = 1Wuyi      forIinchXrange (1,40): theName ='name_%d'%I -Score = Random.randint (30,100) Wu         ifScore <= 50: -chart_dict['0-50'] + = 1 About         elifScore>50 andScore<=60: $chart_dict['50-60'] + = 1 -         elifScore>60 andScore<=70: -chart_dict['60-70'] + = 1 -         elifScore>70 andScore<=80: Achart_dict['70-80'] + = 1 +         elifScore>80 andScore<=90: thechart_dict['80-90'] + = 1 -         Else: $chart_dict['90-100'] + = 1 the  the         ifScore > 60: the table.write_string (i, 0, name) theTable.write_number (i, 1, score) -         Else: in table.write_string (i, 0, name, Color_format) theTable.write_number (i, 1, score, Color_format) the  AboutTable.write_datetime (I, 2, Date.today (), date_format) theMM = mm + 1 the  the     #Generate chart Data +row = 1 -      forKvinchChart_dict.items (): theTable.write_string (Row, 3, K, Font_format)BayiTable.write_number (Row, 4, V, Font_format) therow = Row+1 the  - Xlwt_chart (xl_obj,table) -     #working with Formulas theTable.write_formula (mm,1,'=average (B2:B40)') the     #insert a picture with a link theTable.insert_image ('D20'R'/home/mywork/pythonchina/cto51_log/bd_logo12.png',{'URL':'https://www.baidu.com'}) the  -     #Close Excel Handle the xl_obj.close () the  the if __name__=='__main__':94Xlwt_run ()

No class writing is used, just improvisation.

Application of Python Xlsxwriter Library generation chart

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.