1 #! /usr/bin/env python
2 #coding=utf-8
3 from xlrd import open_workbook #匯入xlrd中的模組open_workbook
4 wb = open_workbook("Book1.xls") #開啟一個xls檔案,並賦值給wb
5 print wb.nsheets #列印這個xls檔案的sheet數
6 print wb.sheet_names() #用unicode格式返回xls檔案中所有sheet的名稱
7 for sheet in wb.sheets(): #迭代
8 print sheet #列印結果
9 # <xlrd.sheet.Sheet object at 0x01BDCAD0>
10 # <xlrd.sheet.Sheet object at 0x01BDCC70>
11 # <xlrd.sheet.Sheet object at 0x01BDCC90>
12 for sheet_index in range(wb.nsheets): #迭代
13 print wb.sheet_by_index(sheet_index) #列印結果同上
14
15 for sheet_name in wb.sheet_names(): #迭代
16 print wb.sheet_by_name(sheet_name) #列印結果同上
1 #coding:gb2312
2 from xlrd import open_workbook,cellname
3 wb = open_workbook("Book1.xls")
4 print wb.nsheets
5
6 for aa in range(wb.nsheets): #列印所有的sheet的名稱
7 sheet = wb.sheet_by_index(aa)
8 print sheet.name
9
10
11 sheet1 = wb.sheet_by_index(0) #列印index為0的sheet的名稱
12 print sheet1.name
13
14
15 print sheet1.nrows #列印sheet1的總行數
16 print sheet1.ncols #列印sheet1的總列數
17
18 for row_index in range(sheet1.nrows): #迭代每一行
19 for col_index in range(sheet1.ncols): #迭代每一列
20 print cellname(row_index ,col_index ),'-', #cellname()為儲存格的名稱
21 print sheet1.cell(row_index ,col_index ).value #cell()為儲存格的內容
22