[python-win32] Python and Excel via Cron

Jd H pyth_ora at yahoo.com
Thu Nov 22 00:05:08 CET 2007


I am posting test.py again below:
   
  <<<
  from win32com.client import Dispatch 
   
  myExcel = Dispatch('Excel.Application')
myExcel.Workbooks.Add('C:\Python24\\test.xls')
myExcel.Workbooks.Add('C:\Python24\\macro.XLA')
myExcel.Run('Macro1')
myExcel.Application.DisplayAlerts = False
myExcel.ActiveWorkbook.SaveAs('C:\Python24\\test.xls')
myExcel.Quit()
  >>>
   
  Cygwin cron calling test.py works great on my Windows XP PC with Python 2.4 version. From windows 2000 terminal server with same Python 2.4 version, it works perfectly as it is supposed to if run via Python 2.4. But if called via cygwin cron (like it needs to be), it fails per below error:
   
  Traceback (most recent call last):
  File "c:\Python24\test.py", line 4, in ?
    myExcel.Workbooks.Add('C:\Python24\\test.xls')
  File "C:\Python24\Lib\site-packages\win32com\client\dynamic.py", line 496, in __getattr__
    raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: Excel.Application.Workbooks

  Thanks,
  JD
  
Tim Roberts <timr at probo.com> wrote:
  Jd H wrote:
> Hello,
> 
> I have the below python code which opens an excel sheet, runs a macro
> then save the file. When run from python it runs perfectly. Code is
> below. File name is test.py
> 
> <<< 
> from win32com.client import Dispatch
> 
> myExcel = Dispatch('Excel.Application')
> myExcel.Visible = 1
> myExcel.Workbooks.Add('C:\Python24\test.xls')

I don't believe this ever ran correctly as you have posted it. This
will not open a file called "test.xls" in the directory "Python24". 
Instead, this will open a file in the root called "Python24est.xls"
with a tab character in the name. You HAVE to be careful about
backslashes in Python. Either double the backslashes, replace them with
forward slashes, or use a raw string ( r'c:\Python24\test.xls' ).

> myExcel.Workbooks.Add('C:\Python24\macro.XLA')
> myExcel.Run('Macro1')
> myExcel.Application.DisplayAlerts = False
> myExcel.ActiveWorkbook.SaveAs('C:\Python24\test.xls')
> myExcel.Quit()
> >>> 
> 
> When run from cron, it fails at line 3 when it opens the file with
> below message:
> 
> Traceback (most recent call last):
> File "c:\Python24\test.py", line 20, in ?
> jd.Workbooks.Add('C:\Python24\test.xls')
> File "C:\Python24\Lib\site-packages\win32com\client\dynamic.py",
> line 496, in __getattr__
> raise AttributeError, "%s.%s" % (self._username_, attr)
> AttributeError: Excel.Application.Workbooks
> 
> Any ideas how to resolve this?

What utility are you using to schedule this? Are you actually using
Cygwin's cron, or the "at" command, or the standard "Schedule Tasks"
thing, or what? What operating system are you using?

Most of the task scheduler tools run as a service. By default, a
service cannot launch a GUI application. Also, in some cases, the
services run as a user that has restricted rights. Tell us a little
more about how you are doing this, and perhaps we can suggest how to
accomplish it.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.

_______________________________________________
python-win32 mailing list
python-win32 at python.org
http://mail.python.org/mailman/listinfo/python-win32


       
---------------------------------
Be a better sports nut! Let your teams follow you with Yahoo Mobile. Try it now.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20071121/e5c469aa/attachment.htm 


More information about the python-win32 mailing list