Write a Python program to plot the Line chart in MS Excel Sheet using XlsxWriter module to display the annual net income of the companies mentioned below.



Microsoft Excel is a software program produced by Microsoft that allows users to organize, format and calculate data with formulas using a spreadsheet system. This software is part of the Microsoft Office suite and is compatible with other applications in the Office suite.

Microsoft Excel terminology

  • Workbook ->The workbook refers to an Excel spreadsheet file. The workbook houses all of the data that you have entered and allows you to sort or calculate the results. A workbook that is available to be viewed and edited by multiple users on a network is known as a Shared Workbook. A single workbook is saved in a file with the .xlsx extension.

  • Worksheet -> Within the workbook is where you'll find documents called worksheets. Also known as spreadsheets, you can have multiple worksheets nestled in a workbook. Tabs at the bottom of the of the screen will indicate which of your worksheets you are currently working on. This is also known as an active worksheet or active sheet.

  • Cell -> A cell is a rectangle or block housed in a worksheet. Any data that you want to enter into your worksheet must be placed in a cell. Cells can be color coded, display text, numbers and the results of calculations, based on what you want to accomplish. An Active Cell is one that is currently opened for editing.

  • Columns and Rows -> Columns and Rows refer to how your cells are aligned. Columns are aligned vertically while rows are aligned horizontally.

  • Column and Row headings -> These headings are the lettered and numbered gray areas found just outside of columns and rows. Clicking on a heading will select the entire row or column. You can also alter the row height or column width using the headings.

  • Workspace -> Much like worksheets in a workbook, a workspace allows you to open numerous files simultaneously.

  • Ribbon -> Above the workbook is a section of command tabs called the Ribbon. A multitude of options are found behind each tab of the ribbon

  • Cell Reference -> A cell reference is a set of coordinates that identifies a specific cell. It's a combination of letters and numbers. A5, for example, would point to the cell located where column A and row 5 intersect.

  • Cell Range -> A Cell range is a collection of cells that have been identified as a group based on a variety of criteria. By using a colon (:) between cell references, Excel can determine the range, also known as an array. A range in a row, for example, could look like A1:C1, telling the formula to look at the cells in a row between A1 and C1, while B4:D9 would tell the formula to look at all cells in a box bounded by columns B and D and rows 4 and 9.



About XlsxWriter

XlsxWriter is a Python module for writing files in the Excel 2007+ XLSX file format.

XlsxWriter can be used to write text, numbers, formulas and hyperlinks to multiple worksheets and it supports features such as formatting and many more, including:

  • 100% compatible Excel XLSX files.
  • Full formatting.
  • Merged cells.
  • Defined names.
  • Charts.
  • Autofilters.
  • Data validation and drop down lists.
  • Conditional formatting.
  • Worksheet PNG/JPEG images.
  • Rich multi-format strings.
  • Cell comments.
  • Memory optimisation mode for writing large files.


  • It supports more Excel features than any of the alternative modules.
  • It has a high degree of fidelity with files produced by Excel. In most cases the files produced are 100% equivalent to files produced by Excel.
  • It has extensive documentation, example files and tests.
  • It is fast and can be configured to use very little memory even for very large output files.


  • It cannot read or modify existing Excel XLSX files.


Description of the methods used in the program

xlsxwriter.Workbook() -> The Workbook() constructor is used to create a new Excel workbook with a given filename.

add_worksheet() -> The add_worksheet() method adds a new worksheet to a workbook. At least one worksheet should be added to a new workbook.

add_format() -> The add_format() method can be used to create new Format objects which are used to apply formatting to a cell. You can either define the properties at creation time via a dictionary of property values or later via method calls.

write_row() -> The write_row() method can be used to write a list of data in one go. Write a row of data starting from (row, col).

write_column() -> The write_column() method can be used to write a list of data in one go. Write a column of data starting from (row, col).

add_chart() -> This method is use to create a new chart object that can be inserted into a worksheet via the insert_chart() Worksheet method. For plotting the Line chart on an excel sheet, use add_chart() method with type line keyword argument of a workbook object.

insert_chart() -> This method can be used to insert a chart into a worksheet. A chart object is created via the Workbook add_chart() method where the chart type is specified.

add_series() -> Add a data series to a chart. In Excel a chart series is a collection of information that defines which data is plotted such as values, axis labels and formatting. The series options that can be set are:

  • values: This is the most important property of a series and is mandatory for every chart object. This option links the chart with the worksheet data that it displays. The data range can be set using a formula such as "=Sheet1!$B$2:$B$9" or a list with a sheetname, row and column such as ["Sheet1", 1, 2, 8, 2].

  • categories: This sets the chart category labels. The category is more or less the same as the X axis. In most chart types the categories property is optional and the chart will just assume a sequential series from 1..n.

  • name: Set the name for the series. The name is displayed in the formula bar. For non-Pie/Doughnut charts, it is also displayed in the legend. The name property is optional and if it isn't supplied it will default to Series 1..n. The name can also be a formula such as =Sheet1!$B$1 or a list with a sheetname, row and column such as ["Sheet1", 0, 2].

set_style() -> Set the chart style type. The set_style() method is used to set the style of the chart to one of the 48 built-in styles available on the 'Design' tab in Excel. The style index number is counted from 1 on the top left. The default style is 2.

set_title() -> The set_title() method is used to set properties of the chart title.

set_x_axis() -> The set_x_axis() method is used to set properties of the X axis.

set_y_axis() -> The set_y_axis() method is used to set properties of the Y axis.

close() -> The workbook close() method writes all data to the xlsx file and closes it.


In [8]:
# import xlsxwriter module
import xlsxwriter

# Workbook() takes one, non-optional, argument
# which is the filename that we want to create.
# The file is present in the working directory.
workbook = xlsxwriter.Workbook("line_chart.xlsx")

# The workbook object is then used to add new
# worksheet via the add_worksheet() method.
worksheet = workbook.add_worksheet()  # Defaults to Sheet1.

# Create a new Format object to formats cells
# in worksheets using add_format() method .

# The properties of a cell that can be formatted include:
# fonts, colors, patterns, borders, alignment and number formatting.
# here we create bold format object .
bold = workbook.add_format({"bold": 1})

# create a data list .
headings = ["Year", "Microsoft", "Alphabet", "Amazon"]

data = [
    [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017],
    [18.76, 23.15, 16.98, 21.86, 22.07, 12.19, 16.8, 21.2],
    [8.376, 9.706, 10.179, 12.733, 14.136, 16.348, 19.478, 12.662],
    [1.152, 0.631, 0.139, 0.274, 0.241, 0.596, 2.371, 3.033],

# Write a row of data starting from 'A1'
# with bold format .
worksheet.write_row("A1", headings, bold)

# Write a column of data starting from
# 'A2', 'B2', 'C2' 'D2' respectively .
worksheet.write_column("A2", data[0])
worksheet.write_column("B2", data[1])
worksheet.write_column("C2", data[2])
worksheet.write_column("D2", data[3])

# Create a chart object that can be added
# to a worksheet using add_chart() method.

# here we create a line chart object .
chart = workbook.add_chart({"type": "line"})

# Add a data series to a chart
# using add_series method.

# Configure the first series.
# = Sheet1!$A$1 is equivalent to ['Sheet1', 0, 0].

# note : spaces is not inserted in b / w
# = and Sheet1, Sheet1 and !
# if space is inserted it throws warning.
        "name": "=Sheet1!$B$1",
        "categories": "=Sheet1!$A$2:$A$9",
        "values": "=Sheet1!$B$2:$B$9",

# Configure a second series.
# Note use of alternative syntax to define ranges.
# [sheetname, first_row, first_col, last_row, last_col].
        "name": ["Sheet1", 0, 2],
        "categories": ["Sheet1", 1, 0, 8, 0],
        "values": ["Sheet1", 1, 2, 8, 2],

# Configure a third series.
# Note use of alternative syntax to define ranges.
# [sheetname, first_row, first_col, last_row, last_col].
        "name": ["Sheet1", 0, 3],
        "categories": ["Sheet1", 1, 1, 8, 1],
        "values": ["Sheet1", 1, 3, 8, 3],

# Add a chart title
chart.set_title({"name": "Company Profit Analysis"})

# Add x-axis label
chart.set_x_axis({"name": "Year"})

# Add y-axis label
chart.set_y_axis({"name": "Profit (in Billions)"})

# Set an Excel chart style.

# add chart to the worksheet with given
# offset values at the top-left corner of
# a chart is anchored to cell D2 .
worksheet.insert_chart("D2", chart, {"x_offset": 25, "y_offset": 10})

# Finally, close the Excel file
# via the close() method.