bk2014111825 2021-09-15 19:11 采纳率: 80%
浏览 90
已结题

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())

  • 写回答

1条回答 默认 最新

  • CSDN专家-HGJ 2021-09-15 22:01
    关注

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

    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])
    
    

    如有帮助,请点击采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月19日
  • 已采纳回答 10月11日
  • 创建了问题 9月15日