Richard Flynn
Richard Flynn

Reputation: 11

In openpyxl, how can I set a range using the 'get_column_letter' function?

In my Python Code, I am trying to set a range of ('E3:H28'). However, the 'H' column letter needs to be a variable. The following code will not run for me:

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

Upvotes: 1

Views: 2544

Answers (1)

Charlie Clark
Charlie Clark

Reputation: 19527

The simplest solution is to install the beta of openpyxl 2.4 which has min_col, min_row, max_col, max_row parameters for ws.iter_rows():

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

Otherwise you should use string formatting to create the range:

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

Upvotes: 1

Related Questions