2011-06-13 6 views
0

내가 COM을 사용하여 파이썬에서 엑셀 파일을 열려고 시도하고, 오류를 찾을 수 없습니다 파일을 잡으려고 노력하고 있어요 :방법 파일 열기에 pywin32com 예외를 잡을 수

내가 먼저 잡기 시도 IO 오류 :

try: 
    output = xl.Workbooks.Open(Params.workbookName) 
except IOError as reason: 
    print reason 
    exit() 
나는이 시도 그래서 논리적으로

com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u"'asdf.xlsx' could not be found. Check the spelling of the file name, and verify that the file location is correct.\n\nIf you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.", u'C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM', 0, -2146827284), None)

:이 파일을 찾을 수없는 문제가있는 경우

그러나 COM이 IO 오류를 발생시키지 않습니다, 대신 뭔가라는 com_error을 제기

01 23,516,
try: 
    output = xl.Workbooks.Open(Params.workbookName) 
except com_error as reason: 
    print reason 
    exit() 

하지만 ...

NameError: global name 'ComError' is not defined 

답변

1

시도 :

from pythoncom import com_error 

하고 except 블록에 그것을 잡을

관련 문제