0

I am defining a pandas dataframe by reading an excel file saved locally:

 df_names = pd.read_excel(r'path/NAMES.xlsx', "Sheet1")

I am using pandas. However the above line leads to this error:

raise ValueError("File is not a recognized excel file")

Any help would be greatly appreciated. I realise the fact it is a xlsx file complicates things but i don't really want to use xls files.

daniel
  • 37
  • 2
  • 9
  • It looks that you are reading it with pandas not through openpyxl – Jayvee Jun 05 '21 at 11:02
  • @Jayvee Yeah you're correct, made the correction above – daniel Jun 05 '21 at 11:09
  • From the error message it's clear that there is a problem with the file: all XLSX files are zip files. – Charlie Clark Jun 05 '21 at 13:25
  • @CharlieClark I have now changed the file itself from scratch, pretty certain it is a valid xlsx file now. Changed the code to: df_names = xlrd.open_workbook(r'path/NAMES.xlsx') is this correct? It sill yield the same error: ValueError: File is not a recognized excel file – daniel Jun 05 '21 at 21:04

1 Answers1

1

If it is a valid xlsx file then then issue could be related to the pandas version; you can try by explicitly using openpyxl. Assuming you have openpyxl installed then please try:

df_names = pd.read_excel(r'path/NAMES.xlsx',sheet_name= "Sheet1", engine='openpyxl')
Jayvee
  • 10,670
  • 3
  • 29
  • 40
  • I made this edit the code but it leads to this error: zipfile.BadZipFile: File is not a zip file. – daniel Jun 05 '21 at 12:33
  • what pandas version would be ideal to solve this issue? – daniel Jun 05 '21 at 12:34
  • I believe the latest version should be fine but this error makes me think that it actually is something wrong with the file – Jayvee Jun 05 '21 at 12:43
  • there is a good discussion about this here: https://stackoverflow.com/questions/65250207/pandas-cannot-open-an-excel-xlsx-file – Jayvee Jun 05 '21 at 12:45
  • I have now changed the file itself from scratch, pretty certain it is a valid xlsx file now. Changed the code to: df_names = xlrd.open_workbook(r'path/NAMES.xlsx') is this correct? It sill yield the same error: ValueError: File is not a recognized excel file – daniel Jun 05 '21 at 21:03
  • Please try opening it with ooenpyxl – Jayvee Jun 05 '21 at 21:44