赞
踩
读取excel文件,需要先安装xlrd库,在命令行窗口运行pip install xlrd== 2.0.1命令
import datetime import xlrd # 打开excel表格 data_excel = xlrd.open_workbook('mb.xls') # 获取所有sheet名称 names = data_excel.sheet_names() # 获取book中的sheet工作表的三种方法,返回一个xlrd.sheet.Sheet()对象 table = data_excel.sheets()[0] # 通过索引顺序获取sheet # table = data_excel.sheet_by_index(sheetx=0) # 通过索引顺序获取sheet # table = data_excel.sheet_by_name(sheet_name='Sheet1') # 通过名称获取 # excel工作表的行列操作 n_rows = table.nrows # 获取该sheet中的有效行数 n_cols = table.ncols # 获取该sheet中的有效列数 print("行数", n_rows) print('列数=', n_cols) row_list = table.row(rowx=0) # 返回某行中所有的单元格对象组成的列表 cols_list = table.col(colx=0) # 返回某列中所有的单元格对象组成的列表 # 返回某行中所有单元格的数据组成的列表 row_data = table.row_values(0, start_colx=0, end_colx=2) print('第一行的数据', row_data) # 返回某列中所有单元格的数据组成的列表 cols_data = table.col_values(0, start_rowx=0, end_rowx=None) row_lenth = table.row_len(0) # 返回某行的有效单元格长度 # excel工作表的单元格操作 row_col = table.cell(rowx=0, colx=5) # 返回单元格对象 row_col_data = table.cell_value(rowx=0, colx=0) # 返回单元格中的数据 for r in range(n_rows): for c in range(n_cols): cell_value = table.cell_value(r, c) if not cell_value: break # print('第', r, '行的数据是', cell_value) if 0 < r < n_rows and 0 < c: date_tuple = xlrd.xldate_as_tuple(cell_value, data_excel.datemode) date_value = datetime.date(*date_tuple[:3]) cell_value = date_value.strftime('%Y/%m/%d') print('第', r, '行的数据是', cell_value)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。