Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python xlrd - error handling

Is there a way to handle xlrd errors in the program? I have a complex problem with thousands of excel files. I am trying to parse the list of files, open each spreadsheet and determine if the spreadsheet has a specific tab or sheet. Create a new list of the files with the matching sheet name. However, I keep getting dropped out due to errors. First was ragged, turned out to be a file with multiple periods (ie. Myfile.New.Jan2013.xls). I temporarily renamed that one and but now it dropped out - Workbook is encrypted. I have no control over the files - they were supplied to me. And there are far too many to go through one at a time and change. So I would like to just skip or create a list of the files that are faulty and continue with my loop. I have been googling, but have not found anything on error handling within xlrd yet. Any suggestions?

Thanks!

like image 551
FredG Avatar asked Mar 05 '14 22:03

FredG


People also ask

What is xlrd and XLWT?

xlrd and xlwt are libraries used to retrieve data from Excel files or to save (generated or processed) data back to Excel files using Python.

What is the use of xlrd?

xlrd provides functions to filter sheets from an excel file and not read the whole workbook. Data from a specific cell of an excel file can be read in python by using cell_value() which takes row and column number of that sepcific cell as its input.

What is xlrd package?

xlrd is a module that allows Python to read data from Excel files.

What is xlrd in Python?

Python xlrd | What is python xlrd? | How to use python xlrd? Python xlrd retrieves data from a spreadsheet using the xlrd module. It is used to read, write, or modify data. Furthermore, the user may be required to navigate sheets on certain criteria, as well as modify some rows, and columns and perform other tasks.

How do you handle exceptions in Python?

When an error occurs, or exception as we call it, Python will normally stop and generate an error message. These exceptions can be handled using the try statement: Since the try block raises an error, the except block will be executed. This statement will raise an error, because x is not defined:

Can I use xlrd to read from Microsoft Excel files?

You may notice if you open your file in Microsoft Excel and save it, you will be able to use xlrd to read and no exception will be raised. This is because Excel already fixed the issues for you when saving the file.

How many code examples are there for xlrd?

The following are 18 code examples for showing how to use xlrd.XLRDError () . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.


1 Answers

without having more information all I can do is suggest a general

exceptions = []

for excel_file in excel_file_list:
    try:
        some_things
    except Exception, e:
       exceptions.append((excel_file,e.message))

This modification will tell you what the problem was for each file

like image 113
PyNEwbie Avatar answered Sep 30 '22 04:09

PyNEwbie