模块:xlrd
可以pip install xlrd
打开一个excel:
table = xlrd.open_workbook(filename)
获取一个sheet
first_sheet = table.sheets()[0] # or
first_sheet = table.sheet_by_index(0) # or
first_sheet = table.sheet_by_name(u'Sheet1')
获取一个表的行数和列数
row = sheet_object.nrows
col = sheet_object.ncols
代码
import xlrd
def open_excel(path):
table = None
try:
table = xlrd.open_workbook(filename=path, encoding_override='utf-8')
except Exception as error:
print('[-]error in open_excel()', error)
finally:
return table
def get_sheet_by_index(excel, index):
try:
return excel.sheet_by_index(index)
except Exception as error:
print('[-]error in get_sheet()', error)
def output_sheet(sheet):
row = sheet.nrows
col = sheet.ncols
for i in range(row):
if i == 0:
continue
for j in range(col):
if j == 0:
continue
print(int(sheet.row_values(i)[j]), end=' ')
print()
path = 'C:/Users/Administrator/Desktop/workcall.xlsx'
table = open_excel(path=path)
first_sheet = get_sheet_by_index(excel=table, index=0)
output_sheet(sheet=first_sheet)
写操作等我用到了再来继续添加