Search code examples
python-3.xopenpyxlexport-to-excel

TypeError: "worksheet" object not callable while writing on an existing workbook sheet


I am trying to write a list into a column of an existing workbook sheet using openpyxl- in column A, from row13 onwards- and I am having this error "TypeError: “worksheet” object not callable". Here is the code I have used-

variable_name = ["a","b","c"]
workbook = load_workbook("file.xlsx")
ws = workbook['9. Family Assignment']
for i in range(0,len(variable_name)):
    ws(row =i+13,col = 'A').value = variable_name[i]

The excel file I am using https://drive.google.com/file/d/1X62EnywUWXwsv5KErbUCUqq4D-ceK_Nh/view?usp=sharing

Any help on this regards is highly appreciated.


Solution

  • "TypeError: “worksheet” object not callable"
    

    tells you, that the variable you are treating like a function is not a function.

    In this line

    ws(row =i+13,col = 'A')#.value = variable_name[i]
    

    you are calling ws as a function but it is a worksheet. Worksheets are not callable.

    According to the documentation here openpyxl.readthedocs.io/en/stable/ you can do ws['A2'] to access cell A2. Or in your case ws['f"A{i+13}"'].value.

    (Don't know which version you are using. Assumed latest stable 3.0.6. )