Python: xlrd (Read Excel File)

(Last Updated On: )

In this tutorial I will show you how to read an excel file in Python.

Installation

  1. pip install xlrd

Open The Workbook

  1. import xlrd
  2.  
  3. my_excel = (r'C:\path\to\file')
  4. wb = xlrd.open_workbook(my_excel)

Select Sheet

  1. # Select the first sheet. If you want to select the third just change to (3)
  2. sheet = wb.sheet_by_index(0)

Get Data In Column

  1. #This loops through all the rows in that sheet
  2. for i in range(sheet.nrows):
  3. # if the value isn't empty then print it out.
  4. if sheet.cell_value(i, 0) != '':
  5. print(sheet.cell_value(i, 0))

Get all the Column Header

  1. #This loops through all the rows in that sheet
  2. for i in range(sheet.ncols):
  3. # if the value isn't empty then print it out.
  4. if sheet.cell_value(0, i) != '':
  5. print(sheet.cell_value(0, i))