AttributeError: 'tuple" object has no attribute 'value'

AttributeError: 'tuple" object has no attribute 'value'

我想从 excel sheet 中检索特定的单元格值,这是我的代码。

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:“元组”对象没有属性 'value'。我不太了解 python 中的循环。在 C 中,我们可以像 for(i=2;i<=row_count ;i++)。我不知道我的循环是否在代码中是最新的。有人能帮我吗?

您正在做的 sheet['Ax'] 似乎有问题。您将无法以这种方式使用 x

尝试修改您的索引,如下代码所示。

title=sheet['A' + str(x)].value

上述解决方案对我的情况不起作用。我最终使用了以下内容:

title=sheet.cell(row=x, column=1).value

我有一个不同的问题导致了这个错误。我的代码试图获取第 0 行中单元格的值(我 运行 循环太远了)。

第 0 行不存在(工作表从第 1 行开始),因此出现 AttributeError: 'tuple' object has no attribute 'value' 错误。

一般结构:ws['A1'] = 'Whosebug'

注意:尝试打印单元格名称以检查是否将列名称作为 'A1'、'A3' 像这样传递或仅作为 'A'.

阅读这份精美的文档:https://openpyxl.readthedocs.io/en/stable/tutorial.html