Reputation: 53
I would like to iterate over sheets 3 to 9. So a total of 7 sheets will be looped over. How to I specify they sheets to loop over?
import openpyxl
wb = openpyxl.load_workbook('DemoFile.xlsx')
for sheets 3 to 9 in wb.worksheets:
print(Sheets)
Upvotes: 1
Views: 675
Reputation: 7431
Just use range
to specify which sheets to iterate based on the index of the sheet. openpyxl
uses 0-based indexes, so sheet 3 has an index of 2.
import openpyxl
wb = openpyxl.load_workbook('DemoFile.xlsx')
for n in range(2,9):
print(wb.worksheets[n])
Output:
<Worksheet "Sheet3">
<Worksheet "Sheet4">
<Worksheet "Sheet5">
<Worksheet "Sheet6">
<Worksheet "Sheet7">
<Worksheet "Sheet8">
<Worksheet "Sheet9">
Upvotes: 1