Openpyxl for cell.value and cell.internal_value error: 'int' object has no attribute 'internal_value'

I am trying to print the value of the cell in excel using openpyxl. However, I get the following error: 'int' object has no attribute 'internal_value' when I attempt to do print(cell.internal_value) and 'int' object has no attribute 'value' when I try to do print(cell.value).

I have done the following:

for cell in range(2,18):
    print(int(cell.value))
    print(cell.internal_value)
    print(cell.value)

Any ideas?

1 answer

  • answered 2020-06-01 15:25 dstrants

    You are looping with range so cell is actually an integer and not a sheet's cell that is why you are not able to use the above attributes.

    You should first try to create a new spreadsheet or load an existing one and then access its cells.

    from openpyxl import load_workbook
    wb = load_workbook(filename = 'name_of_your_spreadsheet.xlsx')
    # Let's say you want to print the A column
    for cell in wb['A2 A18']:
        print(int(cell.value)) #this might not work
        print(cell.internal_value)
        print(cell.value)
    

    if you want to use range in any case you can do something like this

    from openpyxl import load_workbook
    wb = load_workbook(filename = 'name_of_your_spreadsheet.xlsx')
    # Let's say you want to print the A column
    for i in range(2,18):
        cell = ws3.cell(column=0, row=i)
        print(int(cell.value)) #this might not work
        print(cell.internal_value)
        print(cell.value)