python读取excel

来源:互联网 发布:显示网络存在安全隐患 编辑:程序博客网 时间:2024/05/18 02:48

模块:xlrd
可以pip install xlrd


打开一个excel:

table = xlrd.open_workbook(filename)

获取一个sheet

first_sheet = table.sheets()[0]  # orfirst_sheet = table.sheet_by_index(0) # orfirst_sheet = table.sheet_by_name(u'Sheet1')

获取一个表的行数和列数

row = sheet_object.nrowscol = sheet_object.ncols

代码

import xlrddef 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 tabledef 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)

写操作等我用到了再来继续添加

0 0
原创粉丝点击