openpyxl获取最后一列和倒数第二列所有值的问题

问题描述:

背景:获取excel表格最后一列值时报错,IndexError: tuple index out of range
诉求:因为excel表格是可维护的,要求在更改后的excel中也能获取最后一列的值。要求提供正确的代码片段

def get_Product_Category_value():
    y_3_data_Product_Category = []
    for i in range(8, 36):
        cell = ws[ws.max_column][i]
        y_3_data_Product_Category.append(cell.value)
    return y_3_data_Product_Category
print(get_Product_Category_value())

获取表格最后一列数据,可用如下代码:

import openpyxl
wb=openpyxl.load_workbook('t913.xlsx')
ws=wb.active
col_data=[]
for col in ws.columns:
    col_data.append([x.value for x in col])
print(col_data[-1][8:36])

如有帮助,请点击采纳。