Opening file in Excel

dsavitsk dsavitsk at e-coli.net
Sun Aug 18 16:31:03 EDT 2002


have you tried using the whole file path? i.e. -> 'C:\\my\\path\\mytest.xls'
also, instead of throwing the file name at XL, see if the file exists first.
>>> import os.path
>>> if os.path.exists(filename):
...    lf.xlBook = self.xlApp.Workbooks.Open(filename)

-d


"Lemniscate" <d_blade8 at hotmail.com> wrote in message
news:7396d2b2.0208161046.7b18638f at posting.google.com...
> Hi everybody,
>
> I did a couple of searches of c.l.p and will do some more, but I
> haven't been able to see anybody else with this problem.  Basically, I
> am using the EasyExcel script from 'Python Programming on Win32' and I
> am attempting to open an excel file.  I get a message that the file is
> not present.  If I then open Excel, open the file, then close it, I
> can open it.  Below is the traceback error I get.  You'll notice that
> it opens fine and I can retrieve data the second time.  It's really
> confusing me.  Also, I will post the relevant portion of code (and
> just that section).  I need to be able to open, and process, excel
> files.  Oh, the file is on the desktop, and Pythonwin is running in
> the same directory.  Using the full path of the file results in the
> exact same thing.  Thanks.
>
> Chris
>
> PS- I've decided to ignore it for now, but I cannot seem to open a
> password protected file, even when I add the Password= flag to the
> open function (I open the file, but still get the password prompt at
> Excel, so I can't seem to automate opening protected files).  If you
> know off-hand why, could you let me know (no rush here, I need to be
> able to open files first).  Thanks.
>
> PythonWin 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on
> win32.
> Portions Copyright 1994-2001 Mark Hammond (mhammond at skippinet.com.au)
> - see 'Help/About PythonWin' for further copyright information.
> >>> myxl = EasyExcel.EasyExcel('mytest.xls')
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
>   File "C:\Documents and Settings\ckarim\Desktop\EasyExcel.py", line
> 11, in __init__
>     self.xlBook = self.xlApp.Workbooks.Open(filename)
>   File "win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x3.py",
> line 14716, in Open
> com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel',
> "'mytest.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 on the File
> menu, make sure that the file has not been renamed, moved, or
> deleted.", 'C:\\Program Files\\Microsoft
> Office\\Office\\1033\\xlmain9.chm', 0, -2146827284), None)
>
> ## Here, I manually open then close the file, then it works
>
> >>> myxl = EasyExcel.EasyExcel('mytest.xls')
> >>> myxl.GetRange(1,1,40,1)
> ((None,), (u'Target',), (u'ES',), (u'Mouse #',), (u'TC #',), (None,),
> (u'Target',), (u'ES',), (u'Mouse #',), (u'TC #',), (None,),
> (u'Target',), (u'ES',), (u'Mouse #',), (u'TC #',), (None,),
> (u'Target',), (u'ES',), (u'Mouse #',), (u'TC #',), (None,),
> (u'Target',), (u'ES',), (u'Mouse #',), (u'TC #',), (None,),
> (u'Target',), (u'ES',), (u'Mouse #',), (u'TC #',), (None,),
> (u'Target',), (u'ES',), (u'Mouse #',), (u'TC #',), (None,),
> (u'Target',), (u'ES',), (u'Mouse #',), (u'TC #',))
> >>>
>
>
> import win32com.client
> class EasyExcel:
>     """From "Programming Python on Win32."
>     Multiple functions for use in manipulating data in and out of
>     MS Excel.  Functions may be added at any time."""
>     def __init__(self, filename = None):
>         self.xlApp = win32com.client.Dispatch("Excel.Application")
>         self.xlApp.Visible = 1
>         if filename:
>             self.filename = filename
>             self.xlBook = self.xlApp.Workbooks.Open(filename)
>         else:
>             self.xlBook = self.xlApp.Workbooks.Add()
>             self.filename = ''





More information about the Python-list mailing list