Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Chart in Excel File by Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "Python how to write charts in Excel files", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve Python writing charts in Excel documents" article.

Let's first take a look at the functions you need (all of which are executed through the workbook).

Function name description parameter add_chart () create chart object {type: style} add_series () define the data dictionary to be displayed set_title () define chart title string add_series parameter description value categories display title = Sheet1 displaying Avatar

Shert1: workbook name

$A1: 1:1:A$4 represents the data displayed by the title values from the first value to the fourth value in line A = Sheet1 displaying B1: 1:1:B$4

Shert1: workbook name

$B1: 1:1:B$4 indicates that the title name table name is displayed from the first value to the fourth value in row A.

$A$1:$A$4 indicates that the title is displayed from the first value to the fourth value in line A.

The style name of the chart describes the area area style sheet bar bar style sheet column bar style sheet line line style sheet pie pie chart style sheet doughnut ring style sheet scatter scatter style sheet stock inventory style sheet rader radar style sheet combined with study.xlsx file to display the student scores in the form of a table chart

Here we continue to use the case of "write the contents of the study.xlsx file in the project to write.xlsx" in the previous chapter

The code example is as follows:

# coding:utf-8import xlsxwriterimport xlrd# excel = xlsxwriter.Workbook ('write.xlsx') # initialize excel object # book = excel.add_worksheet (' study') # add workbook # # title = ['name', 'gender', 'age', 'grade'] # define the first line of write.xlsx # # for index Data in enumerate (title): # for uses enumeration functions to write the contents of title to the first line of "write.xlsx" # book.write (0, index) Data) # excel.close () def read (): # define a read function to read the "study.xlsx" file result = [] excel = xlrd.open_workbook ('study.xlsx') book = excel.sheet_by_name (' student's manual') for i in book.get_rows (): content = [] for j in i: content.append (j.value) result.append (content) return resultdef write (content): # define a write function to write the content read to "study.xlsx" to the "write.xlsx" file excel = xlsxwriter.Workbook ('write.xlsx') book = excel.add_worksheet (' study') for index Data in enumerate (content): print (data) for sub_index, sub_data in enumerate (data): # print (sub_index, sub_data) book.write (index, sub_index Sub_data) book1 = excel.add_worksheet ('student grade') # define "student grade" workbook data = [# define data list to store the grade and quantity of student grades ['excellent', 'good', 'medium', 'poor'], [15,25,55 5]] book1.write_column ('A1Qing, data [0]) # write_column () function Add by row (that is, line A1 adds the contents of the 0 index of the data list) book1.write_column ('B1' Data [1]) chart = excel.add_chart ({'type':' column'}) # defines the chart style as a columnar stylesheet chart.add_series ({# add the data to be displayed 'categories':' = student grade! $A1 excel.add_chart 'A4',' values':'= student grade! $B1purl 'B4' 'name':' score ratio'}) chart.set_title ({'name':' score ratio Chart'}) # define chart title book1.insert_chart ('A10grade, chart) # place chart in the A10 position of the workbook corresponding to the excel table excel.close () if _ name__ = =' _ main__': result = read () write (result)

The running results are as follows:

Expansion: try pie chart and bar style chart again

The code example is as follows:

# coding:utf-8import xlsxwriterimport xlrd# excel = xlsxwriter.Workbook ('write.xlsx') # initialize excel object # book = excel.add_worksheet (' study') # add workbook # # title = ['name', 'gender', 'age', 'grade'] # define the first line of write.xlsx # # for index Data in enumerate (title): # for uses enumeration functions to write the contents of title to the first line of "write.xlsx" # book.write (0, index) Data) # excel.close () def read (): # define a read function to read the "study.xlsx" file result = [] excel = xlrd.open_workbook ('study.xlsx') book = excel.sheet_by_name (' student's manual') for i in book.get_rows (): content = [] for j in i: content.append (j.value) result.append (content) return resultdef write (content): # define a write function to write the content read to "study.xlsx" to the "write.xlsx" file excel = xlsxwriter.Workbook ('write.xlsx') book = excel.add_worksheet (' study') for index Data in enumerate (content): print (data) for sub_index, sub_data in enumerate (data): # print (sub_index, sub_data) book.write (index, sub_index Sub_data) book1 = excel.add_worksheet ('student grade') # define "student grade" workbook data = [# define data list to store the grade and quantity of student grades ['excellent', 'good', 'medium', 'poor'], [15,25,55 5]] book1.write_column ('A1Qing, data [0]) # write_column () function Add by row (that is, line A1 adds the contents of the 0 index of the data list) book1.write_column ('B1' Data [1]) chart = excel.add_chart ({'type':' column'}) # defines the chart style as a columnar stylesheet chart.add_series ({# add the data to be displayed 'categories':' = student grade! $A1 excel.add_chart 'A4',' values':'= student grade! $B1purl 'B4' 'name':' score'}) chart.set_title ({'name':' score ratio chart'}) # define chart title book1.insert_chart ('A10') Chart) # place chart in the A10 position of the workbook corresponding to the excel form chart = excel.add_chart ({'type':' bar'}) chart.add_series ({'categories':' = student grade! $A1VR A4', 'values':' = student grade! $B1VOB4' 'name':' score percentage'}) chart.set_title ({'name':' score ratio Chart'}) book1.insert_chart ('A25grade, chart) chart = excel.add_chart ({' type': 'pie'}) chart.add_series ({' categories':'= student grade! $A1 name': grade A4 grade, 'values':' = student grade! $B1name': grade B4' 'name':' score ratio'}) chart.set_title ({'name':' score ratio Chart'}) book1.insert_chart ('A40, chart) excel.close () if _ _ name__ =' _ _ main__': result = read () write (result)

The running results are as follows:

The above is about the content of this article on "how Python can write charts in Excel documents". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report