python之xlrd模块

记录用xlrd模块分别读取单Sheet和多Sheet

 1 import xlrd
 2 
 3 '''读取单Sheet版
 4 指定文件编码,读取Excel文件;
 5 读取指定的某个Sheet所有内容并输出所有内容'''
 6 xlrd.Book.encoding = 'utf-8'
 7 rd = xlrd.open_workbook(r'C:\Users\Administrator\Documents\testdata.xls')
 8 print(rd)
 9 # print(rd.nsheets, type(rd.nsheets))
10 sheet = rd.sheet_by_index(0)
11 print(rd.sheet_names())
12 rows = sheet.nrows
13 for i in range(rows):
14     print(sheet.row_values(i))
15 '''读取多Sheet版
16 读取文件的所有Sheet并输出所有内容'''
17 rd1 = xlrd.open_workbook(r'C:\Users\Administrator\Documents\testdata-release1.xls', formatting_info=True)
18 print(rd1)
19 # print(rd1.nsheets, type(rd1.nsheets))
20 for j in range(rd1.nsheets):
21     sheet1 = rd1.sheet_by_index(j)
22     print(sheet1.merged_cells)
23     rows1 = sheet1.nrows
24     cols = sheet1.ncols
25     # print(sheet1, type(sheet1), rows1, type(rows1))
26     for k in range(rows1):
27         print(sheet1.row_values(k))
View Code

 

你可能感兴趣的:(python之xlrd模块)