Mack M. Mack M. - 6 months ago 33
Python Question

Openpyxl gives an inconsistent error message

I have the following function that will read from an excel workbook with the openpyxl library:

import openpyxl

def read_excel(path):
excel_workbook = openpyxl.load_workbook(path, read_only = True)
# other logic
return None


I can call that function like this:

read_excel("C:/Users/anon/Desktop/Current Projects/Test Files/Test.xlsm ")


And it returns this error:

openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support .xlsm file
format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,
.xltx,.xltm


That error message confuses me. It's telling me that it doesn't support the
.xlsm
file format, and that it supports the
.xlsm
file format. The file opens just fine in excel, why won't openpyxl read my Excel file?

Answer

There is an extra whitespace character in the error message after .xlsm. Remove the whitespace character at the end of the path string you call the function with, and the function runs without error.

read_excel("C:/Users/anon/Desktop/Current Projects/Test Files/Test.xlsm")