简体   繁体   中英

Accessing the cell objects value openpyxl at row level

I would like to make a hash key of the value in each row of the first column of a spreadsheet, then load the rest of the row values into a dictionary (think that is correct python terminology) but in order to do this I would need to access the value of the row and cell.

I have searched various examples and tutorials, and I have got close to what I want, but seems I can't access the data I need to create a hashkey. I get the error:

hashString = row.cell(row=baseincr,column=1)
AttributeError: 'tuple' object has no attribute 'cell'

I am failing to access the correct object, but have no idea what type of object I should be using. I have tried using the entire sheet using ".active" to get the entire worksheet, but this also fails with the same error.

totalcols = tuple(sheet['A12':'J100'])
baseincr = 0
hashString = ""
dataHash = {hashString:[]}

for row in totalcols:
    baseincr += 1
    hashString = row.cell(row=baseincr,column=1)
    for cell in row:
        dataHash.setdefault(hashString,[]).append(cell.value)

OK, I found the solution.

for row in totalcols:
    baseincr += 1
    hashString = sheet.cell(row=baseincr,column=1).value
    for cell in row:
        dataHash.setdefault(hashString,[]).append(cell.value)

You can directly access the value in the cell by giving the row and column, but it is an attribute of the sheet, not a tuple or row.

OK, I found the solution.

for row in totalcols:
    baseincr += 1
    hashString = sheet.cell(row=baseincr,column=1).value
    for cell in row:
        dataHash.setdefault(hashString,[]).append(cell.value)

You can directly access the value in the cell by giving the row and column, but it is an attribute of the sheet, not a tuple or row.

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