I am trying to open an excel workbook and iterate through each of the worksheets in a loop. Here is first loop:
wb = openpyxl.load_workbook('snakes.xlsx')
for i in wb.worksheets:
i = i + 1
for ws in wb.worksheets:
img = openpyxl.drawing.image.Image(folder + str(var) + '.png')
Your code snippets seem to show a fundamental misunderstanding of how the
for loop works in Python.
To loop through each sheet, you were on the right track:
wb = openpyxl.load_workbook('test.xlsx') for sheet in wb.worksheets: # do stuff with "sheet" pass
The variable in the
for loop (
sheet in my example,
i in yours) is the member of the list (
wb.worksheets): it is not an integer index. In your example, you immediately overwrite the value of
i with 0 in every loop and thus do not have a sheet to work with.
It is also worth noting
get_sheet_names() is called from the workbook object, so there is no need to call it within the for loop:
>>> wb.worksheets [<Worksheet "Sheet1">, <Worksheet "Sheet2">, <Worksheet "Sheet3">]
Finally, your second "nested for loop" (which isn't even nested) is correct, except it saves the new Excel file every loop, which is wasteful.
Since you indicate that the worksheet name is the same as PNG name, you can just call the attribute
title for the worksheet when finding the image.
Below should be a working example:
wb = openpyxl.load_workbook('snakes.xlsx') for ws in wb.worksheets: img = openpyxl.drawing.image.Image(ws.title + '.png') ws.add_image(img, 'K1') wb.save('new.xlsx')