0

When I try to open an excel file by calling EXCEL itself from python, I get error. How can I fix that?

Thanks in advance.

The code is:

    from win32com.client import Dispatch
    xl = Dispatch('Excel.Application')
    wb = xl.Workbooks.Open(r"data\Modules.xls")       

And the error is:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u"'data\Modules.xls' 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)

5
  • 2
    Is the python code file in the directory where the data directory exists? Try giving full path of the xls file instead of relative path. Commented May 4, 2011 at 7:12
  • It is in the same directory. It must be this way. I shouldnt give the full path. Commented May 4, 2011 at 14:04
  • I know that's not what you are asking for but you should try the xlrd module instead of using win32com. Will make your life easier. Commented May 4, 2011 at 17:39
  • Can I open an excel file in EXCEL itself by using xlrd? Commented May 5, 2011 at 5:14
  • [tested, working as expected!!! ](stackoverflow.com/a/76919456/12395911) Commented Aug 17, 2023 at 9:02

5 Answers 5

9

Use os.path.abspath() to convert file system paths to absolute. The current working directory of yout Python and Excel process is not the same.

http://docs.python.org/library/os.path.html

Sign up to request clarification or add additional context in comments.

3 Comments

What path should I supply to that function?
When I use "wb = xl.Workbooks.Open(os.path.abspath(r"data\Modules.xls"))", I dont get error. But I cant see any Excel file opened. Even if I see an excel instance in task manager.
os.path.join(os.getcwd(), "path, "to", "your", "file.xls"))
0

I believe the reason why you must specify a full path to the file is because you are interacting with Excel through a COM interface. This is not the same as calling CreateProcess. The COM interface tells excel to open a file, however, the path is passed in relation to the working directory of the excel.exe process.

Comments

0

I have tried many cases about:

  • raw path
  • using os.path.abspath
  • absolute path

the following is the result:

(1)Failed Cases:

#[1] Fail
# xlsPath = "chart_demo.xls";
# wb = xl.Workbooks.open(xlsPath); #pywintypes.com_error

#[2] Fail
# xlsPath = "D:\tmp\tmp_dev_root\python\excel_chart\chart_demo.xls";
# absPath = os.path.abspath(xlsPath);
# print "absPath=",absPath; #absPath= D:\tmp\tmp_dev_root\python\excel_chart\        mp      mp_dev_root\python\excel_chart\chart_demo.xls
# wb = xl.Workbooks.open(absPath); #pywintypes.com_error

#[3] Fail
# xlsPath = "D:\tmp\tmp_dev_root\python\excel_chart\chart_demo.xls";
# normalPath = os.path.normpath(xlsPath);
# print "normalPath=",normalPath; #normalPath= D:  mp      mp_dev_root\python\excel_chart\chart_demo.xls
# wb = xl.Workbooks.open(normalPath); #pywintypes.com_error

#[4] Fail
# rawPath = r"chart_demo.xls";
# wb = xl.Workbooks.open(rawPath); #pywintypes.com_error

(2)Successfull Cases:

#[5] OK
# xlsPath = "chart_demo.xls";
# absPath = os.path.abspath(xlsPath);
# print "absPath=",absPath; #absPath= D:\tmp\tmp_dev_root\python\excel_chart\chart_demo.xls
# wb = xl.Workbooks.open(absPath); #OK

#[6] OK
# rawPath = r"D:\tmp\tmp_dev_root\python\excel_chart\chart_demo.xls";
# wb = xl.Workbooks.open(rawPath); # OK

Comments

0

@Shanshal I don't know if you are still looking for the answer After opening the excel if you can't see the file, write the below code

xl.Visible = True

Comments

0

The path to excel file should be absolute and py file, excel file should be in different folder. I was also getting the same error when I used above measures problem got solved. In case of relative path you need to convert it to absolute.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.