簡體   English   中英

無法使用xlrd打開xlsx文件

[英]Unable to open xlsx file with xlrd

我收到xlrd-0.7.1中不支持的錯誤文件。

該文件以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'

該文件不存在。

在調用函數之前檢查文件的位置:

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

一個可能更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