UnicodeDecodeError when trying to operate xlsx file with openpyxl

Viewed 24

I'm trying to make my program import data from xlsx file. Here's the part of code:

def Volunteer_list_show():
    volunteer_list = load_workbook('Volunteer_list.xlsx')
    surnames = volunteer_list['A']
    names = volunteer_list['B']
    ...
    identificator = volunteer_list['K']

And have this type (UnicodeDecodeError) of error message:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xec in position 1: invalid continuation byte

I tried this solution from other thread:

def Volunteer_list_encoding_check(volunteer_list):
    encodings = ['utf-8', 'iso-8859-1', 'windows-1251', 'windows-1252']
    for encoding in encodings:
        try:
            decoded_file = volunteer_list.decode(encoding)
        except UnicodeDecodeError:
            pass
        else:
            decoded_file = decoded_file.encode("utf-8")
            break
    else:
        print('Not approved encoding')  

But, I have the same error as first time, even if def Volunteer_list_show() is deleted. Opening of workbook via load_workbook function alone does not cause problem. Other code checked and does not cause problem.

How to decode-encode xlsx here in UTF-8?

1 Answers

I've found my mistake: you have to work with active sheet to take cells or their values, so I've just added this code:

volunteer_list_act = volunteer_list.active

It works now.

Related