[python-win32] Runing an Excel macro from python

Python python at telefonica.net
Sun Jun 1 14:32:31 EDT 2003


Hi everybody, I´m newbie with python and my english is not very fluent so i´ll try to explain my problem as better as i can::

I want to open an excel  document and save it as .CSV extension. I have a macro for that job, saved into the personal macros book called "PRESONAL.xls" 
If i do double click on PERSOANL.xls, excel is visible and I can execute the macro (for save as .CSV).
The problem begins with python :( i do this:

import win32com.client
import os
import tempfile
xl= win32com.client.Dispatch("Excel.Application")
xl.Visible=1
xl.Workbooks.Add('D:\PERSONAL.xls')

and then excel is open but hanged on. The error message on the Python interpreter says: 

File "C:\PYTHON22\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
File "D:\pruebas-macro.py", line 6, in ?
    xl.Workbooks.Add('D:\PERSONAL.xls')
File "C:\PYTHON22\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x2\Workbooks.py", line 30, in Add
    ret = self._oleobj_.InvokeTypes(181, LCID, 1, (9, 0), ((12, 17),),Template)
com_error: (-2147417851, 'Server launches an exception.', None, None)

What i´m doing wrong? maybe i have to call to "pythoncom.CreateGuid()" or use IDispatch instead of Dispatch?
 I have been searching for a full example script in python calling excel but i only found a few example lines (not a full program). I would very pleased if anybody could tell me where I can  found a full example program.

Thanks to everybody.












More information about the Python-win32 mailing list