I tried VBA and it didn't work either. Some sort of Application Object error
(don't remember the exact error message). However, I managed to create a
pivot table in Excel by means of an ODBC connection. So I gave up trying to
figure out why it didn't work.
The following code created the Pivot table in Excel, all that is left to do is
do define the row, column, and data fields. It should be no problem to
program this in Python. Otherwise the user can set up the table in Excel
himself.

from win32com.client import constants, Dispatch
xl = Dispatch('Excel.Application')
xl.Workbooks.Add()
xl.ActiveWorkbook.SaveAs(r'd:\pivottable.xls')
ws = xl.ActiveWorkbook.ActiveSheet
pc = xl.ActiveWorkbook.PivotCaches().Add(constants.xlExternal)
pc.Connection = r'ODBC;DSN=MS Access Database;DBQ=d:\test.mdb;'
pc.CommandType = constants.xlCmdSql
pc.CommandText = 'SELECT * FROM whatever'
pc.CreatePivotTable(ws.Cells(3,1),'pivot')
xl.ActiveWorkbook.Save()

Thanks for all the help
peter

> >Traceback (most recent call last):
> >File "<stdin>", line 1, in ?
> >File "C:\Python24\lib\site-packages\win32com\client\__init__.py", line
> > 463, in __setattr__
> >self._oleobj_.Invoke(*(args + (value,) + defArgs))
> >pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None,
> >None, 0, -2146827284), None)
> >
> >As you can see, I get the PivotCache object, but I need to set the
> > Recordset in order to use rs. I get the same error message when want to
> > set pc.OptimizeCache to True. Honestly, I have no idea what is going on,
> > so if someone could help me?
>
> Well, -2146827284 is 800A03EC, which is some kind of generic "access
> denied" error within Excel.
>
> Can you run this EXACT sequence in VBA within Excel?


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

Reply via email to