Python 操作Excel文件


from openpyxl import load_workbook
# 打开Excel文件
wb = load_workbook("test.xlsx")
# 定位第一个sheet
sheet = wb['Sheet1']
# 获取第1行第1列的单元格的值
print("第1行第1列的单元格的值 {}".format(sheet.cell(1,1).value))
# 获取第1行第2列的单元格的值
print("第1行第2列的单元格的值 {}".format(sheet.cell(1,2).value))
# 获取最大行
print("最大行 {}".format(sheet.max_row))
# 获取最大列
print("最大列 {}".format(sheet.max_column))
# 循环读取excel的单元格的值
for i in range(1, sheet.max_row+1):
for j in range(1, sheet.max_column+1):
print("第{0}行第{1}列的值为{2}".format(i, j, sheet.cell(i, j).value))
原文地址:https://www.cnblogs.com/LtTest/p/13300152.html