2

I have the following script which was immitated from here ( http://pythonexcels.com/python-excel-mini-cookbook/ ):

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open('words.xlsx')

and it returns the following error ( full traceback )

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    wb = excel.Workbooks.Open('words.xlsx')
  File "C:directory\Python35\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x7\Workbooks.py", line 78, in Open
    , Converter, AddToMru, Local, CorruptLoad)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "'words.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.", 'xlmain11.chm', 0, -2146827284), None)

When I alternatively use openpyxl's functions to open the workbook there is no issue (referenced this https://automatetheboringstuff.com/chapter12/ ) . The python file and the excel file are in the same folder together. Am I calling something inappropriately?

I am certain that the file is spelled correctly ( words.xlsx ) and that it is in the same folder as the python file.

Any thoughts would be appreciated.

2
  • Your file and script on same directory and file got 'R-W'(read-Write) permission ? Commented Jul 5, 2016 at 20:44
  • Yes, it does have read/write permission. If it is of any value I am using Python 3.5 on a Windows 7 machine. Commented Jul 6, 2016 at 13:18

2 Answers 2

5

Try this:

import win32com.client as win32
import os
excel = win32.gencache.EnsureDispatch('Excel.Application')
path =  os.getcwd().replace('\'','\\') + '\\'
wb = excel.Workbooks.Open(path+'words.xlsx')

Excepted a path error, not module or system error.

'xlmain11.chm' is empty, so don't need this.

Be careful when using escape characters on path-string. Script and work file are in the same directory!

Hope that helps

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

2 Comments

This worked after I changed "getcwdu" to "getcwd" , I am not sure if it is a python version difference or something else but your suggestion worked perfectly. Thank you!
Should be corrected by now, had the same issue and its clearly a typo
0

Have you tried openpyxl, it's very easy to use, reading and writing excel files is no trouble

from openpyxl import Workbook 

And initialize as

wb = Workbook()
ws = wb.active()

And you can start reading and writing right away

2 Comments

In my post I state I attempted to use openpyxl and it works without any issue ( I agree with you! ) but i am not able to systematically fill in cells like I can with win32com. I refernce the cells as sheet[x,y] with x being an int and y being an int rather than having to consider actual column names ( A , B, C etc ).
Okay sorry. So the problem is solved with win32.com right, good luck for with your project.

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.