如何从包中导入异常(Openpyxl)?

时间:2016-08-09 12:44:05

标签: python exception-handling openpyxl

我正在尝试捕获我正在使用的包(Openpyxl)的工作表错误异常。我尝试像from openpyxl.utils import SheetTitleException一样导入异常,但我收到错误"ImportError: cannot import name SheetTitleException"。当我尝试使用from openpyxl.utils import *导入它时,收到错误NameError: global name 'SheetTitleException' is not defined

我确定我输错了,但我不确定我哪里出错了。

Here is the documentation on exceptions for Openpyxl.

以下是我用来捕获异常的代码:

try:
    bdws = bdwb[finalBDSheetName]
except SheetTitleException:
    messageBox("Invalid sheet title. Check your sheet title and try again.")
    return

2 个答案:

答案 0 :(得分:5)

您链接的网页标题为“openpyxl.utils.exceptions”。

因此你应该这样做:

from openpyxl.utils.exceptions import SheetTitleException

答案 1 :(得分:1)

如果它与我已经完成的其他模块异常处理一样,应该是

from openpyxl.utils.exceptions import SheetTitleException

然后使用它

except SheetTitleException as e:
    # do something