Mack M. Mack M. - 5 months ago 24x
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,

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


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")