简体   繁体   English

显示正在由xlrd,python处理的文件名。 Book类没有文件名属性

[英]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. 我正在尝试做一些我认为应该很简单的事情,但似乎并不作为xlrd Book Class的属性存在。

While parsing all of the xlsx files in a directory, I want to log which errors exist in which file. 解析目录中的所有xlsx文件时,我想记录哪个文件中存在哪些错误。 In order to do this, I need to print the filename being processed. 为此,我需要打印正在处理的文件名。

GOAL: Print name of file being processed by xlrd. 目标:打印由xlrd处理的文件的名称。 ie: "filename.xlsx" in example below 即:在下面的示例中为“ filename.xlsx”

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". 这将输出“ _xlnm._FilterDatabase”。 I want to print "filename.xlsx". 我想打印“ filename.xlsx”。 The documentation of the Book Class doesn't have a simple way to do this. Book Class的文档没有执行此操作的简单方法。 http://www.lexicon.net/sjmachin/xlrd.html#xlrd.Book-class 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对象传递了文件名并进行了打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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