本文共 1483 字,大约阅读时间需要 4 分钟。
#!/usr/bin/python## -*- coding: utf-8 -*-import xlrdimport sysreload(sys)sys.setdefaultencoding("utf-8")# 打开exceltable = xlrd.open_workbook('/home/hly/hly/test.xls')# excel 地步表格的名称sheetName = table.sheet_names()print(sheetName)# 获取sheet对象print (table.sheets())# 获取sheet总数print(table.nsheets)# 通过索引获取,例如打开第二个sheet表格sheet = table.sheet_by_index(1)print (sheet)# 通过sheet名称获取,例如打开第二个sheet表格# sheet = table.sheet_by_name("Sheet")# print (sheet)# 获取名称print sheet.name# 获取行数rows = sheet.nrowsprint (rows)# 获取列数cols = sheet.ncolsprint (cols)# 获取第一行的数据one_content = str(sheet.row_values(0)).replace('u\'', '\'').decode("unicode-escape")print (one_content)# 获取第一行表格的内容以及内容的类型one_content = str(sheet.row(0)).replace('u\'', '\'').decode("unicode-escape")print (one_content)# 获取第一列的数据one_col_content = str(sheet.col_values(0)).replace('u\'', '\'').decode("unicode-escape")print one_col_content# 单元格的类型table_content = sheet.cell_type(2,2)print (table_content)# 单元格的内容和类型table_content = sheet.cell(2, 2)print (table_content)# 单元格的内容table_content = sheet.cell_value(2, 2)print (table_content)# 根据行数遍历出整个表格content_list = []for i in range(rows): content_list.append(sheet.row_values(i))# 处理list中文乱码case_list = str(content_list).replace('u\'', '\'').decode("unicode-escape")print (case_list)
当使用xlrd 读取excle 表格的时候需要注意xlrd 的版本
xlrd 版本低于或者等于1.2.0 的时候 读取excel 不支持中文名称的excel 但是确实 以.xls 和 .xlsx 结尾的 excel
xlrd 版本是最新版本的时候目前最新的版本为2.0.1 支持中为名称的excel 但是 不支持.xlsx 结尾的excel ,只支持.xls结尾的excel
转载地址:http://vrfif.baihongyu.com/