读取xlrd表格文件
2021-08-23 本文已影响0人
S_jie
import xlrd, os
base_dir = os.path.dirname(__file__)
path = os.path.join(base_dir, 'bbq.xlsx')
def read_excel():
"""读取excel"""
# 打开文件
workbook = xlrd.open_workbook(path, formatting_info=False)
# 根据sheet索引或者名称获取sheet内容
sheet1 = workbook.sheet_by_index(0)
one = lambda x, y: sheet1.cell_value(x, y)
sheet2 = workbook.sheet_by_index(1)
two = lambda x, y: sheet2.cell_value(x, y)
total = sheet1.nrows
for i in range(total):
print('{}'.format('='*50))
print('表格1第1个数据为:{}'.format(one(i, 0)))
print('表格1第2个数据为:{}'.format(one(i, 1)))
print('表格1第3个数据为:{}'.format(one(i, 2)))
print('{}'.format('*'*50))
print('表格2第1个数据为:{}'.format(two(i, 0)))
print('表格2第2个数据为:{}'.format(two(i, 1)))
print('表格2第3个数据为:{}'.format(two(i, 2)))
if __name__ == "__main__":
read_excel()