在openpyxl中,如何使用'get_column_letter'函数设置范围?

时间:2016-06-10 06:19:01

标签: python openpyxl

在我的Python代码中,我试图设置一个范围('E3:H28')。但是,'H'列字母必须是变量。以下代码不会为我运行:

mo = 3

bc_col = 5 + mo

for row in ws.iter_rows('E3:get_column_letter(bc_col)28'):
    for cell in row:
        ws_blnd1[cell.coordinate] = ws[cell.coordinate].value

1 个答案:

答案 0 :(得分:1)

最简单的解决方案是安装openpyxl 2.4的beta版,其中包含ws.iter_rows()的min_col,min_row,max_col,max_row参数:

for row in ws.iter_rows(min_row=3, min_col=5, max_col=bc_col):
    for cell in row:
        # do something with cell

否则,您应该使用字符串格式来创建范围:

cell_range = "E3:{0}28".format(get_column_letter(bc_col))