简体   繁体   English

无法使用xlrd打开xlsx文件

[英]Unable to open xlsx file with xlrd

I am getting an error file is not support in xlrd-0.7.1. 我收到xlrd-0.7.1中不支持的错误文件。

The file is saved in xlsx format 该文件以xlsx格式保存

Traceback (most recent call last):
  File "C:\Users\jawed\workspace\test\Excelproject.py", line 8, in <module>
    workbook=xlrd.open_workbook(file_location)
  File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 425, in open_workbook
    on_demand=on_demand,
  File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 878, in biff2_8_load
    f = open(filename, open_mode)
IOError: [Errno 2] No such file or directory: 'C:\\Users\\jawed\\workspace\\IAMarks.xls'

The file doesn't exist. 该文件不存在。

Check the location of the file before calling the function: 在调用函数之前检查文件的位置:

import os
if os.path.isfile(file_location):
     workbook = xlrd.open_workbook(file_location)
else:
    # tell the user they've done something wrong

A possibly more Pythonic way to do it (see EAFP ) is in a try / except block: 一个可能更Python化的方式(请参阅EAFP )在try / except块中:

try:
     workbook = xlrd.open_workbook(file_location)
except IOError as error:
    print(error)
    # tell the user they've done something wrong

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

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