简体   繁体   中英

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

I want to retrieve specific cell values from an excel sheet, and here is my code.

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)

I get the following error. AttributeError: 'tuple" object has no attribute 'value'. I dont know much about the loops in python. In C, we can something like for(i=2;i<=row_count;i++). I dont know if my loop is current in the code. Could anyone help me?

I had a different problem that caused this error. My code was attempting to get the value of a cell in row zero (I ran the loop one row too far).

Row zero doesn't exist (sheets begin at row 1), hence the AttributeError: 'tuple' object has no attribute 'value' error.

Problem seems to be with sheet['Ax'] that you are doing. You won't be able to use x this way.

Try modifying your indexes like following in the code.

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

General structure : ws['A1'] = 'Stackoverflow'

NOTE : Try to print the cell name to check weather you are passing column name as 'A1', 'A3' like this or only 'A'.

Read this Becautiful Documentaion : https://openpyxl.readthedocs.io/en/stable/tutorial.html

The above solution did not work in my case. I ended up using the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM