文章目录 [ 隐藏 ]
xlrd模块简介
python的xlrd模块主要用以读取和修改Excle文件。
下载地址:https://pypi.python.org/pypi。
xlrd模块常用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
data = xlrd.open_workbook('excelFile.xls') #打开Excel文件读取数据 table = data.sheets()[0] #获取一个工作表,通过索引顺序获取 table = data.sheet_by_index(0) #通过索引顺序获取 table = data.sheet_by_name(u'Sheet1')#通过名称获取 #获取整行和整列的值(数组) table.row_values(i) table.col_values(i) #获取行数和列数 nrows = table.nrows ncols = table.ncols #循环行列表数据 for i in range(nrows ): print table.row_values(i) #单元格 cell_A1 = table.cell(0,0).value cell_C4 = table.cell(2,3).value #使用行列索引 cell_A1 = table.row(0)[0].value cell_A2 = table.col(1)[0].value #简单的写入 row = 0 col = 0 # 类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error ctype = 1 value = '单元格的值' xf = 0 # 扩展的格式化 table.put_cell(row, col, ctype, value, xf) table.cell(0,0) #单元格的值' table.cell(0,0).value #单元格的值' |
xlrd模块使用实例
编写代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
## Filename : lean001.py # author by : www.py40.com python操作Excel文档 import xlrd import xlwt def read_file(Filename): print(Filename) # 打开文件 workbook = xlrd.open_workbook(Filename) # 获取所有sheet print(workbook.sheet_names()) # 根据sheet索引或者名称获取sheet内容 sheet1 = workbook.sheet_by_index(0) # sheet索引从0开始 # 也可以通过名字获取索引 sheet1 = workbook.sheet_by_name('sheet1') print(sheet1.name,sheet1.nrows,sheet1.ncols) # 获取整行和整列的值(数组) rows = sheet1.row_values(1) # 获取第二行内容 cols = sheet1.col_values(2) # 获取第三列内容 print (rows) print (cols) # 获取单元格内容 print (sheet1.cell(1,0)) print (sheet1.cell(1,0).value) print (sheet1.row(1)[0].value) if __name__ == '__main__': read_file(r'E:\python\learn\demo.xls') |
运行结果
1 2 3 4 5 6 7 8 9 10 11 |
E:\python\python_tools.git\trunk\test>hello.py E:\python\learn\demo1.xls ['sheet1'] sheet1 3 4 ['1', '张三', '男', '1990-01-04'] ['性别', '男', '女'] text:'1' 1 1 E:\python\python_tools.git\trunk\test> |
简单写入Excel内容
1 2 3 4 5 6 7 8 |
row = 0 col = 0 # 类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error ctype = 1 value = '单元格的值' xf = 0 # 扩展的格式化 table.put_cell(row, col, ctype, value, xf) table.cell(0,0) #单元格的值' table.cell(0,0).value #单元格的值' |
日期数据的格式处理
我们再处理上面的文件过程中发现,日期显示为浮点数了。这个怎么处理呢?
python读取excel中单元格的内容返回的有5种类型,即上面例子中的ctype:
1 |
ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error |
即date的ctype=3,这时需要使用xlrd的xldate_as_tuple来处理为date格式,先判断表格的ctype=3时xldate才能开始操作。
1 2 3 |
if (sheet.cell(row,col).ctype == 3): date_value = xlrd.xldate_as_tuple(sheet.cell_value(rows,3),book.datemode) date_tmp = date(*date_value[:3]).strftime('%Y/%m/%d') |
合并单元格的处理
读取文件的时候需要将formatting_info参数设置为True,默认是False,所以上面获取合并的单元格数组为空,
1 2 3 4 5 |
>>> book = xlrd.open_workbook(r'E:\python\learn\demo.xls',formatting_info=True,e ncoding_override="utf-8") >>> sheet1 = book.sheet_by_index(0) >>> sheet1.merged_cells [(6, 7, 2, 4), (1, 3, 3, 4)] |
merged_cells返回的这四个参数的含义是:(row,row_range,col,col_range),其中[row,row_range)包括row,不包括row_range,col也是一样。
(6, 7, 2, 4)的含义是:第6到7行(不包括6),2到4列(不包括2)合并。
(1, 3, 3, 4)的含义是:第1到3行(不包括1)3到4列(不包括3)合并。
利用这个,可以分别获取合并的两个单元格的内容:
1 2 3 4 |
>>> print(sheet1.cell_value(6,2)) 暂无资料 >>> print(sheet1.cell_value(1,3)) 33242.0 |
未经允许不得转载:Python在线学习 » python模块-xlrd读取excel文件