简体   繁体   中英

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?

Try the simple approach:

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

我只是通过另一个类的Wb对象传递了文件名并进行了打印。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM