Search code examples
python-3.xpandasxlsxxlrd

How to open in memory XLS file using Pandas.read_excel?


I have a file which I am trying to open using Pandas.read_excel. I get an error like this -

invalid file:

InMemoryUploadedFile: file.xlsx (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)


Solution

  • First you need to open your file with xlrd, then pass the result to pandas.read_excel with the parameter engine="xlrd"

    import pandas as pd
    import xlrd
    
    book = xlrd.open_workbook(file_contents=myfile)
    df = pd.read_excel(book,engine='xlrd')
    print(df.head())
    

    here "myfile" is your in memory file