iOSecure iOSecure - 5 months ago 13
Python Question

Increment by one everytime loop runs

How can I make the cell number increase by one every time it loops through all of the sheets? I got it to loop through the different sheets itself but I'm not sure how to add +1 to the cell value.

for sheet in sheetlist:
wsX = wb.get_sheet_by_name('{}'.format(sheet))

ws2['D4'] = wsX['P6'].value


I'm trying to get just the
['D4']
to change to D5,D6,D7.. etc up to 25 automatically.

Answer

You can do it easily like this:

count = 4
for sheet in sheetlist:
    wsX = wb.get_sheet_by_name('{}'.format(sheet))
    ws2['D' + str(count)] = wsX['P6'].value       
    count += 1

To explain this part:

'D' + str(count)

The count is an integer and is casted to a string and then concatenated to the 'D'

Comments