Python Forum

Full Version: Openpyxl.InvalidfileException
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am fairly new, one day old and working on Excel files. I have played around with input functions in console and trying to have users input their file names for selection and saving. It all works well when the name is spelled correctly but I want to catch the exception for wrong name. I have tried reading through openpyxl but the syntax and formatting is eluding me.

wb = xl.load_workbook(input("Select File: "))
sheet = wb["Sheet1"]
if the wrong name is typed at input screen like xxx.xlcfs (not .xlsx as required) the following error comes up

Error:
raise InvalidFileException(msg) openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm
I have tried it like normal Pyhton
    try:
        corrected_price = cell.value * float(input("Enter Value: " +""))
    except ValueError:
        print("Enter 0 before decimal")
    break
but was not working..
Where it says ValueError, you would want InvalidFileException instead.