从特定列开始迭代

Starting iterate from specific column

这是我的 .xlsx 文件:

data.xlsx

from openpyxl import load_workbook

workbook = load_workbook('/path/', read_only=True)
first_sheet = workbook.sheetnames()[0]
worksheet = workbook[first_sheet]

for row in worksheet:
    for cell in row:
        print(cell.value)

如何从A4开始迭代?我需要打印 yearmonth 值。

您可以使用 iter_rows,通过指定偏移量或范围

for row in worksheet.iter_rows('A4:B9'):
    for cell in row:
        print(cell.value)

顺便说一句:sheetnames()[0] 应该是 sheetnames[0]get_sheet_names()[0]