Search code examples
pythonfilenamesxlrd

Display filename being processed by xlrd, python. No filename attribute of Book class


I'm trying to do something that I feel should be very straight forward, but doesn't seem to exist as an attribute to the xlrd Book Class.

While parsing all of the xlsx files in a directory, I want to log which errors exist in which file. In order to do this, I need to print the filename being processed.

GOAL: Print name of file being processed by xlrd. ie: "filename.xlsx" in example below

Example code:

Wb = xlrd.open_workbook ( "./data/excel_files/filename.xlsx" )
print "File being processed is: %s" % Wb.name_obj_list[0].name

This outputs "_xlnm._FilterDatabase". I want to print "filename.xlsx". The documentation of the Book Class doesn't have a simple way to do this. http://www.lexicon.net/sjmachin/xlrd.html#xlrd.Book-class

Any advice?


Solution

  • Try the simple approach:

    for filename in glob('*.xls*'):
        try:
           wb = xlrd.open_workbook(filename)
        except xlrd.XLRDERROR:
           print 'Problem processing {}'.format(filename)