Openpyxl gives an inconsistent error message

Question:

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?

Asked By: MackM

||

Answers:

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")
Answered By: MackM

the same problem bothered me a lot also today, and finally I updated openpyxl from 2.3.2 to 2.3.5, and this problem disappeared.

Although I am using Anaconda, sometimes using pip to update the packages might be a good try.

Answered By: Jinstrong

I’m using PyQt5 and had the same problem. I found that adding _filter fixed the problem. The full line reads:

fileName, _filter = QtWidgets.QFileDialog.getOpenFileName(None, "Lists", "", "xlsx files *.xlsx")
Answered By: Jeff

First, change the cwd(). When passing the file name, you can just copy the name of the file and paste it instead of typing it manually. The error may arise from some undetected nuances.

Answered By: Peter Prah Owusu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.