【发布时间】:2018-03-19 14:59:28
【问题描述】:
我试图从我的 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
【问题讨论】:
标签: python excel python-3.x openpyxl