
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Adding a Chartsheet in an excel sheet using Python XlsxWriter module
In addition to python’s own libraries, there are many external libraries created by individual authors which do a great job of creating additional features in python. Xlsx library is one such library which not only creates excel files containing data from python programs but also creates charts.
Creating Pie Chart
In the below example we will create a pie chart using the xlsxwriter writer. Here we first define a workbook then add a worksheet to it in the next step we define the data and decide on the columns where the data will be stored in an excel file based on those columns we define a pie chart and again are the chat to a specific location in the worksheet.
Example
import xlsxwriter workbook = xlsxwriter.Workbook('pie_chart_example.xlsx') worksheet = workbook.add_worksheet() # Add the data to be plotted. data = [ ['milk', 'fruit', 'eggs', 'grains'], [27,34,12,8] ] worksheet.write_column('A1', data[0]) worksheet.write_column('B1', data[1]) # Create a new chart object. chart = workbook.add_chart({'type': 'pie'}) # Add a series to the chart. chart.add_series({ 'categories': '=Sheet1!$A$1:$A$4', 'values': '=Sheet1!$B$1:$B$4' }) # Insert the chart into the worksheet at a specific position worksheet.insert_chart('C5', chart) workbook.close()
Running the above code gives us the following result
- Related Articles
- Python - Plotting Stock charts in excel sheet using XlsxWriter module
- Python - Plotting Area charts in excel sheet using XlsxWriter module
- Python - Plotting bar charts in excel sheet using XlsxWriter module
- Python - Plotting column charts in excel sheet using XlsxWriter module
- Python - Plotting Combined charts in excel sheet using XlsxWriter module
- Python - Plotting Doughnut charts in excel sheet using XlsxWriter module
- Python - Plotting Line charts in excel sheet using XlsxWriter module
- Python - Plotting Pie charts in excel sheet using XlsxWriter module
- Python - Plotting Radar charts in excel sheet using XlsxWriter module
- Python - Plotting scatter charts in excel sheet using XlsxWriter module
- Plotting stock charts in excel sheet using xlsxwriter module in python
- Python - Plotting charts in excel sheet with Data Tools using XlsxWriter module
- Python - Plotting Different types of style charts in excel sheet using XlsxWriter module
- Python - Plotting an Excel chart with Gradient fills using XlsxWriter module
- Python - Plotting an Excel chart with pattern fills in column using XlsxWriter module

Advertisements