import xlrd
#xlrd2.0版本只能读取xls
#xlrd1.0.2版本能读取xlsx
def read_excel():
# 打开文件
workBook = xlrd.open_workbook(r' ');#excel文件路径
# 1.2 按索引号获取sheet的名字(string类型)
# sheet1Name = workBook.sheet_names()[0];
# print(sheet1Name);
# 2. 获取sheet内容
sheet1_content1 = workBook.sheet_by_index(0); # sheet索引从0开始
# # 按sheet名字获取sheet内容,sheet的名称,行数,列数
# print(sheet1_content1.name,sheet1_content1.nrows,sheet1_content1.ncols);
# 4. 获取整行和整列的值(数组)
rows = sheet1_content1.row_values(3); # 获取第四行内容
cols = sheet1_content1.col_values(1); # 获取第二列内容
print(rows);
print(cols);
# 5. 获取单元格内容(三种方式)
# print(sheet1_content1.cell(1, 0).value);
# print(sheet1_content1.cell_value(1, 1));
# print(sheet1_content1.row(1)[1].value);
#
# # 6. 获取单元格内容的数据类型
# # Tips: python读取excel中单元格的内容返回的有5种类型 [0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error]
# print(sheet1_content1.cell(0, 0).ctype);
if __name__ == '__main__':
read_excel();
python读取excel
最新推荐文章于 2023-03-12 16:02:30 发布