我想从excel工作表中检索特定的单元格值,下面是我的代码。
import openpyxl
file=openpyxl.load_workbook('C:/Users/epeeham/Desktop/xsdi-auto/Test-XSDI.xlsx')
sheet=file.get_sheet_by_name('ExportList')
row_count=sheet.max_row;
for x in range(2, row_count):
title=sheet['Ax'].value
ed=sheet['Bx'].value
if "P" in ed:
edi=ed[1:]
edi1, edi2, edi3=edi.partition('-')
edition=edi1
else:
edi1, edi2, edi3=ed.partition('-')
edition=edi1
n=sheet['Fx'].value
number=n[:15]
print(title + " " + edition + " " + number)
我得到以下错误。 AttributeError:“tuple”对象没有属性“value”。我不太了解python中的循环。在C语言中,我们可以类似于(i=2;i<;=行计数;i++)。我不知道我的循环在代码中是否是最新的。有人能帮我吗?
问题似乎出在你所做的
sheet['Ax']
上。你不能这样使用x
。尝试修改索引,如下代码所示。
title=sheet['A' + str(x)].value
以上的解决方案在我的情况下不起作用。我最终使用了以下方法:
我有一个不同的问题导致了这个错误。我的代码试图获取第0行单元格的值(我将循环运行了一行太远)。
第0行不存在(工作表从第1行开始),因此出现
AttributeError: 'tuple' object has no attribute 'value'
错误。相关问题 更多 >
编程相关推荐