Figure 12-2. Screenshot of workbook in Excel with a chart
Reading from Workbooks
The sister library xlrd is responsible for reading data from spreadsheet files (i.e.,
workbooks):
In [ 19 ]: book = xlrd.open_workbook(path + ‘workbook.xlsx’)
In [ 20 ]: book
Out[20]: <xlrd.book.Book at 0x7f7dabec4890>
Once a workbook is opened, the sheet_names method provides the names of all
Worksheet objects in this particular Workbook object:
In [ 21 ]: book.sheet_names()
Out[21]: [u’first_sheet’, u’second_sheet’]
Worksheets can be accessed via their names or index values:
In [ 22 ]: sheet_1 = book.sheet_by_name(‘first_sheet’)
sheet_2 = book.sheet_by_index( 1 )
sheet_1
Out[22]: <xlrd.sheet.Sheet at 0x7f7dabec4a10>
In [ 23 ]: sheet_2.name
Out[23]: u’second_sheet’
Important attributes of a Worksheet object are ncols and nrows, indicating the number of