Cell coordinates should be provided as a string:
ws['A1'] = 'testing 1-2-3'
Or, if you want to use row and column indexes, use ws.cell().value:
ws.cell(row=1, column=1).value = 'testing 1-2-3'
Answer from alecxe on Stack Overflowhii folks,
i am trying to write data to a excel file.when using for loop,to assign the values to cells,it's giving me the error that 'tuple' object has no attribute 'value' at line 6.
import openpyxl wb=openpyxl.Workbook() ws=wb.active for i in range(10): index="A"+str(i) ws[index]=i
please help me this error.
When the iteration is run, the return object is a tuple (combination of all values in the cells of the given row) for e.g. ('A', '123', 'CD' , 125) will be result for a row with four columns
for copying values from source cell to target cell, you will need to iterate over the coordinates of the cell (row address and column address)
When you iterate over the rows, the code only understands the row and not each cell in the row. Hence the error, "AttributeError: 'tuple' object has no attribute 'value'"
I hope this helps
I found the issue I was iterating through the workbook instead of the tuple I had made.
wb_72C = workbook_beregn['72C']['E8':'G54']
C_72_00_a = workbook_skema_72['C_72_00_a']['D9':'F55']
#Pair the rows
for row1,row2 in zip(C_72_00_a, wb_72C):
#within the row pair, pair the cells
for cell1, cell2 in zip(row1,row2):
#assign the value of cell 1 to the destination cell 2 for each row
cell2.value = cell1.value
So I changed this line from:
for row1,row2 in zip(C_72_00_a, workbook_beregn):
to this line:
for row1,row2 in zip(C_72_00_a, wb_72C):
I hope this someone else in the future.
I am attempting to extract some numerical data from an Excel spreadsheet. I have successfully extracted the cell locations into a tuple, but I am struggling when trying to extract the numbers from those cells.
Code:
from openpyxl import load_workbook
fileName = 'tempdata.xlsx'
wb = load_workbook(fileName)
sheet1 = wb.get_sheet_by_name("Sheet1")
tempData = sheet1['N3':'N11']
print (tempData)this prints:
((<Cell Sheet1.N3>,), (<Cell Sheet1.N4>,), (<Cell Sheet1.N5>,), (<Cell Sheet1.N6>,), (<Cell Sheet1.N7>,), (<Cell Sheet1.N8>,), (<Cell Sheet1.N9>,), (<Cell Sheet1.N10>,), (<Cell Sheet1.N11>,))
instead of numbers. If I change 'N3':'N11' to only 'N3', and print tempData.value, it prints the value I'm looking for.
OS: Windows 10 Python 3.6
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
Aha...solved it
#access cell data
let_us_see = ws.cell(row=proper_index, column=1).value
print(let_us_see)
Instead of using: print(sheet[string_value].value) i looked on the openpyxl forum which suggested another way of accessing the value from the cell. The above code accepts the integer value of proper_index!
Thanks for all your help dudes and dudettes.
If answer still applies, sheet[string_value] always returns a tuple of cells. Hence you have to choose, which cell you want, even if it gives you only a singleton tuple:
import openpyxl
wb = openpyxl.load_workbook('kids.xlsx')
sheet = wb["Sheet1"]
print("Type index of student")
val = int(
input()
)
proper_index = val + 2
print(proper_index)
index_search = "A%s"%(proper_index,)
print(index_search)
print(sheet[string_value][0].value)
PS: Sorry for being a bit late.
Hello!
Trying to create an excel file that can be read for my bachelors project in college and I have a slight problem. I get "AttributeError: 'tuple' object has no attribute 'value'" and the program is really simple and all other people I have found that have problems with it are more complex than mine. I am a beginner to Python so be kind please.
Code:
from openpyxl import load_workbook
wb = load_workbook('Hello_World.xlsx')
ws = wb.active
col_a = ws['A'].value
print(col_a)
Error:
col_a = ws['A'].value
AttributeError: 'tuple' object has no attribute 'value'
The first column of pyxl, is one based, so if you modify your loop to go over range(1,n) your issues should be resolved
Using .format(i) instead of string + str(i) in ur code may work well!
BTW, ur var my_dict get an error .
eg:
for i in range(10):
sheet['A{}'.format(i)].value = 'xx'