我正在尝试从Excel文件中读取一列,直到它到达一个空单元格,然后它需要停止读取。 到目前为止我的代码:
import openpyxl
import os
def main():
filepath = os.getcwd() + "\test.xlsx"
wb = openpyxl.load_workbook(filename=filepath, read_only=True)
ws = wb['Tab2']
for i in range(2, 1000):
cellValue = ws.cell(row=i, column=1).Value
if cellValue != None:
print(str(i) + " - " + str(cellValue))
else:
break;
if __name__ == "__main__":
main()
通过运行此命令,当它遇到一个空单元格时会出现以下错误。有谁知道我怎么能防止这种情况发生。
Traceback (most recent call last):
File "testFile.py" in <module>
main()
cellValue = sheet.cell(row=i, column=1).value
File "C:\Python34\lib\openpyxl\worksheet\worksheet.py", line 353, in cell
cell = self._get_cell(row, column)
File "C:\Python34\lib\openpyxl\worksheet\read_only.py", line 171, in _get_cell
cell = tuple(self.get_squared_range(column, row, column, row))[0]
IndexError: tuple index out of range
答案 0 :(得分:3)
尝试使用max_row获取最大行数。
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('exc_file.xlsx')
ws1 = wb['Sheet1']
for row in range(1,ws1.max_row):
if(ws1.cell(row,1).value is not None):
print(ws1.cell(row,1).value)
或者如果你想在达到空值时停止阅读,你可以简单地:
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('exc_file.xlsx')
ws1 = wb['Sheet1']
for row in range(1,ws1.max_row):
if(ws1.cell(row,1).value is None):
break
print(ws1.cell(row,1).value)
答案 1 :(得分:2)
这说明了我不鼓励使用ws.cell()
阅读工作表的原因之一。使用更高级别的API ws.iter_rows()
会更好。由于性能原因,ws.iter_cols()
无法在只读模式下使用。
for row in ws.iter_rows(min_col=1, max_col=1):
if cell[0].value is None:
break
print("{0}-{1}".format(cell.row, cell.value)
iter_rows应该保证到达行中始终有一个单元格。