Python xlsxwriter library to generate a chart application example, pythonxlsxwriter

Source: Internet
Author: User

Python xlsxwriter library to generate a chart application example, pythonxlsxwriter

There may not be many people using xlsxwriter, but after using it, you will feel that its function makes you sigh, except that you can generate the excel as required.

You can also add various images, such as bar charts, pie charts, and line charts.

Create an xlsx file and add data

Official documents: http://xlsxwriter.readthedocs.org/

Xlsxwriter can operate xls files.

Note: xlsxwriter can only create new files, but cannot modify the original files. If the new file is created with the same name as the original file, the original file will be overwritten.

Install sudo pip install XlsxWriter in Linux

Install pip install XlsxWriter in Windows

Please refer to the self-generated:

 

This section describes the calculation of Data formulas, the insertion of image links, and the generated charts. If you need other functions, you can continue to refer to the library documentation.

Below I will post the source code. I hope you can use it directly if you encounter similar situations at work.

#! /Usr/bin/env python #-*-coding: UTF-8-*-# Author: Eric. yueimport into randomfrom datetime import dateimport collectionsdef xlwt_chart (xl_obj, table): # generate a bar chart column_chart = xl_obj.add_chart ({'type': 'column '}) column_chart.add_series ({'name ': '= sheet1! $ D $1 ', 'category':' = sheet1! $ D $2: $ D $ 7', 'values': '= sheet1! $ E $2: $ E $ 7'}) table. insert_chart ('g2 ', column_chart) # generate the pie chart column_chart2 = xl_obj.add_chart ({'type': 'pie'}) column_chart2.add_series ({'name': '= sheet1! $ D $1 ', 'category':' = 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 '] # generate an ordered dictionary 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'name') table. write_string (0, 1, u'score ') table. write_string (, u'date') table. merge_range ('d1: E1 ', u'score distribution') table. set_column ('C: e', 15) # define the format 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 # generate chart data 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) # use the formula table. write_formula (mm, 1, '= AVERAGE (B2: B40)') # insert an image table with a link. insert_image ('d20', R'/home/mywork/pythonchina/cto51_log/bd_logo12.png ', {'url': 'https: // www.baidu.com '}) # Close excel handle xl_obj.close () if _ name _ = '_ main _': xlwt_run ()

Class writing is not used, just impromptu.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.