Error AttributeError str object has no attribute row

0 votes

I have code:

def add_to_database(object_name, value):
    workbook = openpyxl.load_workbook(DATABASE_FILE)
    worksheet = workbook[DATABASE_SHEET]
    object_row = None
    for row in worksheet.iter_rows(min_row = 1, max_row = worksheet.max_row + 1, values_only = True):
        if row[0] == object_name:
            object_row = row
            break
    if object_row:
        worksheet.cell(row = object_row[0].row, column = 2).value += value
    else:
        last_row = worksheet.max_row
        worksheet.cell(row = last_row, column = 1).value = object_name
        worksheet.cell(row = last_row, column = 2).value = value
    workbook.save(DATABASE_FILE)
    workbook.close()

I get an error: AttributeError: 'str' object has no attribute 'row' on the line: worksheet.cell(row = object_row[0].row, column = 2).value += value

Apr 4, 2023 in Others by narikkadan
• 63,600 points
2,998 views

1 answer to this question.

0 votes

You're attempting to extract the row value from a string, which is an object without a row value. In order to accomplish this, you'll also need to build a loop that takes the row number into account.

import openpyxl

DATABASE_FILE = "your_database_file.xlsx"
DATABASE_SHEET = "your_database_sheet"

def add_to_database(object_name, value):
    workbook = openpyxl.load_workbook(DATABASE_FILE)
    worksheet = workbook[DATABASE_SHEET]
    object_row_num = None
    for row_num, row in enumerate(worksheet.iter_rows(min_row=1, max_row=worksheet.max_row, values_only=True), start=1):
        if row[0] == object_name:
            object_row_num = row_num
            break
    if object_row_num:
        worksheet.cell(row=object_row_num, column=2).value += value
    else:
        last_row = worksheet.max_row + 1
        worksheet.cell(row=last_row, column=1).value = object_name
        worksheet.cell(row=last_row, column=2).value = value
    workbook.save(DATABASE_FILE)
    workbook.close()

answered Apr 4, 2023 by Kithuzzz
• 38,000 points

Related Questions In Others

0 votes
1 answer

Error: No pubspec.yaml file found.

Hi@akhtar, You need to run this command from ...READ MORE

answered Jul 20, 2020 in Others by MD
• 95,460 points
9,061 views
0 votes
0 answers
0 votes
1 answer

Error: No toolchains found in the NDK toolchains folder for ABI with prefix: llvm

Over two years has passed, now and ...READ MORE

answered Feb 11, 2022 in Others by Soham
• 9,710 points
3,151 views
0 votes
1 answer

ImportError: openpyxl is required for loading excel format files

Forget the PsychoPy complications for the time ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 58,020 points
1,122 views
0 votes
1 answer

Openpyxl password protect excel file python

Workbooks can be secured against certain actions, ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,600 points
8,326 views
0 votes
1 answer

How search data in excel with openpyxl?

The method iter_rows in the library has ...READ MORE

answered Oct 2, 2022 in Others by narikkadan
• 63,600 points
7,182 views
0 votes
1 answer

Get column names of Excel worksheet with OpenPyXL in readonly mode

This will print every thing from row ...READ MORE

answered Oct 22, 2022 in Others by narikkadan
• 63,600 points
7,356 views
0 votes
1 answer

Not Like has no effect while looping an Array

you can add parentheses instead... mixing an ...READ MORE

answered Apr 10, 2023 in Others by Kithuzzz
• 38,000 points
532 views
0 votes
1 answer

Type mismatch error when referring to array element by location vba

We utilise MID to parse the string. ...READ MORE

answered Feb 11, 2023 in Others by Kithuzzz
• 38,000 points
773 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP