RSM
RSM

Reputation: 673

Python: Exception handling for ValueError and XLRDError

I am trying to read a excel file where sheetname output does not exist. In that case i want to except that error which comes as ValueError and XLRDError.

Using the below is not working. Do we need to specify both errors of is there some other way?

try:
    df= pd.read_excel("Sample.xlsx",sheet_name='Output', usecols='B,W', skiprows=0)

except NameError:
    print ("Sheet not exists")

Error output is as follows:

ValueError                                Traceback (most recent call last)
~\Anaconda3\lib\site-packages\xlrd\book.py in sheet_by_name(self, sheet_name)
    473         try:
--> 474             sheetx = self._sheet_names.index(sheet_name)
    475         except ValueError:

ValueError: 'Output' is not in list

During handling of the above exception, another exception occurred:

XLRDError                                 Traceback (most recent call last)
<ipython-input-126-7479be6ca02b> in <module>
      1 os.chdir(inputs_path + "/Output")
      2 try:
----> 3     df = pd.read_excel("sample.xlsx",sheet_name='Output', usecols='B,W', skiprows=0)
      4 
      5 except ValueError:

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, verbose, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    332         convert_float=convert_float,
    333         mangle_dupe_cols=mangle_dupe_cols,
--> 334         **kwds,
    335     )
    336 

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in parse(self, sheet_name, header, names, index_col, usecols, squeeze, converters, true_values, false_values, skiprows, nrows, na_values, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    883             convert_float=convert_float,
    884             mangle_dupe_cols=mangle_dupe_cols,
--> 885             **kwds,
    886         )
    887 

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in parse(self, sheet_name, header, names, index_col, usecols, squeeze, dtype, true_values, false_values, skiprows, nrows, na_values, verbose, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    434 
    435             if isinstance(asheetname, str):
--> 436                 sheet = self.get_sheet_by_name(asheetname)
    437             else:  # assume an integer if not a string
    438                 sheet = self.get_sheet_by_index(asheetname)

~\Anaconda3\lib\site-packages\pandas\io\excel\_xlrd.py in get_sheet_by_name(self, name)
     41 
     42     def get_sheet_by_name(self, name):
---> 43         return self.book.sheet_by_name(name)
     44 
     45     def get_sheet_by_index(self, index):

~\Anaconda3\lib\site-packages\xlrd\book.py in sheet_by_name(self, sheet_name)
    474             sheetx = self._sheet_names.index(sheet_name)
    475         except ValueError:
--> 476             raise XLRDError('No sheet named <%r>' % sheet_name)
    477         return self.sheet_by_index(sheetx)
    478 

XLRDError: No sheet named <'Output'>

Upvotes: 0

Views: 1396

Answers (1)

Umar.H
Umar.H

Reputation: 23099

you need to import XLDR Error from the xldr lib.

from xlrd import XLRDError


try:
    df= pd.read_excel("Sample.xlsx",sheet_name='Output', usecols='B,W', skiprows=0)

except XLRDError:
    print ("Sheet not exists")

Upvotes: 1

Related Questions