如何迭代openpyxl中前3列的所有单元格

时间:2019-09-19 03:14:59

标签: python openpyxl

我这样做很麻烦

现在,我可以通过以下方式获取工作表中的所有行:


from openpyxl import *

sheet = load_workbook(location)["Sheet1"]
for i in sheet:
    for j in i:
        print(j.value)

我如何仅在前三列中这样做?

1 个答案:

答案 0 :(得分:1)

我建议使用以下网站:https://openpyxl.readthedocs.io/en/stable/tutorial.html

无论如何,这是代码段:

from openpyxl import *
wb = Workbook()
ws = wb.active
for cell in ws.iter_rows(min_row=1, max_col = 3):
    .
    .
    .

您还可以使用ws.iter_rows来仅访问值

from openpyxl import *
wb = Workbook()
ws = wb.active
for cell in ws.iter_rows(min_row=1, max_col = 3, values_only = True):
    print(cell) if cell else continue