MackM
MackM

Reputation: 3022

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?

Upvotes: 2

Views: 22810

Answers (4)

Peter Prah Owusu
Peter Prah Owusu

Reputation: 1

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.

Upvotes: -1

Jeff
Jeff

Reputation: 57

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

Upvotes: 0

Jinstrong
Jinstrong

Reputation: 95

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.

Upvotes: 1

MackM
MackM

Reputation: 3022

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

Upvotes: 12

Related Questions