📅  最后修改于: 2023-12-03 15:03:23.027000             🧑  作者: Mango
When working with large datasets in Excel using openpyxl, it can be helpful to skip over certain rows when iterating through the data. This can save processing time and make your code more efficient.
openpyxl provides the iter_rows method for iterating through rows in a worksheet. By default, this method starts at the first row and iterates through all rows in the worksheet. However, you can also specify a range of rows to iterate through using the min_row and max_row parameters.
import openpyxl
# load workbook
wb = openpyxl.load_workbook('my_spreadsheet.xlsx')
# select worksheet
ws = wb['Sheet1']
# iterate through rows 2 through 10
for row in ws.iter_rows(min_row=2, max_row=10):
# do something with the row data
pass
To skip over certain rows when iterating through the worksheet, you can use the continue statement within your for loop. For example, if you want to skip over every other row starting with the second row, you can use the following code:
import openpyxl
# load workbook
wb = openpyxl.load_workbook('my_spreadsheet.xlsx')
# select worksheet
ws = wb['Sheet1']
# iterate through rows, skipping every other row starting with the second row
for i, row in enumerate(ws.iter_rows()):
if i == 0:
# skip the first row
continue
if i % 2 == 0:
# skip every other row starting with the second row
continue
# do something with the row data
pass
In this example, we use the enumerate
function to keep track of the index of the current row. We then check if the index is equal to 0, which would indicate the first row, and skip it using the continue
statement. We also check if the index is divisible by 2, which would indicate every other row starting with the second row, and skip it using the continue
statement.
By skipping over certain rows, we can make our code more efficient when working with large datasets in openpyxl.