Read the excel data using xlrd module
suggest changeThe Python xlrd library extracts data from Microsoft Excel (tm) spreadsheet files. As of xlrd 2.0.0 it reads .xls only, not .xlsx.
Installation:-
pip install xlrd
Or you can install from the source distribution on PyPI:
https://pypi.python.org/pypi/xlrd
Reading an excel sheet:- Import xlrd module and open excel file using open_workbook() method.
import xlrd
book=xlrd.open_workbook('sample.xlsx')
Check number of sheets in the excel
Get the sheet based on index. Index 1 is the second sheet:
sheet=book.sheet_by_index(1)
Read the contents of a cell
Get number of rows and number of columns in an excel sheet
num_rows=sheet.nrows
num_col=sheet.ncols
Get excel sheet by name
sheets = book.sheet_names()
cur_sheet = book.sheet_by_name(sheets[0])
xlrd was not installed, and these samples were not run. The lines “Check number of sheets” and “Read the contents of a cell” have no code under them.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents