Openpyxl:似乎无法为我正在尝试做的事情获得正确的语法(阅读下一个单元格)
Openpyxl: can't seem to get the syntax right for what I'm trying to do (read next cell down)
这是我的代码。我只是想打开一个电子表格(目前只有第一列中有信息),然后读取第一个单元格,打印信息,然后沿着这些行循环回到顶部并向下读取下一个单元格。我做错了什么?
import openpyxl
wb = openpyxl.load_workbook('students2.xlsx')
ws = wb.active
PrintNext = True #Starts my while statement
while True :
for i in range(1,300): #I think this is where I'm having an issue?
for j in range(1,2):
StudentID = ws.cell(row=i+1, column=j).value
print(StudentID)
PrintNext = False #This gets it to move on from my while
pass
PrintNext = True #This is to get it to go back to my while
print(StudentID) #This is to test that it has the next cell down
我在此处的答案的帮助下找到了解决方案,但总体上我找到了一个更好的解决方案。
将这些设置为变量:
for i in range(RowX,RowY):
for j in range(ColX,ColY):
StudentID = ws.cell(row=i+1, column=j).value
这样您所做的任何更改(例如 "RowX = RowX + 1")都会在您下次更新 "for" 语句时反映出来!
您可以使用 cell.offset() 方法。
这是我的代码。我只是想打开一个电子表格(目前只有第一列中有信息),然后读取第一个单元格,打印信息,然后沿着这些行循环回到顶部并向下读取下一个单元格。我做错了什么?
import openpyxl
wb = openpyxl.load_workbook('students2.xlsx')
ws = wb.active
PrintNext = True #Starts my while statement
while True :
for i in range(1,300): #I think this is where I'm having an issue?
for j in range(1,2):
StudentID = ws.cell(row=i+1, column=j).value
print(StudentID)
PrintNext = False #This gets it to move on from my while
pass
PrintNext = True #This is to get it to go back to my while
print(StudentID) #This is to test that it has the next cell down
我在此处的答案的帮助下找到了解决方案,但总体上我找到了一个更好的解决方案。
将这些设置为变量:
for i in range(RowX,RowY):
for j in range(ColX,ColY):
StudentID = ws.cell(row=i+1, column=j).value
这样您所做的任何更改(例如 "RowX = RowX + 1")都会在您下次更新 "for" 语句时反映出来!
您可以使用 cell.offset() 方法。