Dealing with Excel

McBooCzech petr at tpc.cz
Tue Oct 18 17:55:48 EDT 2005


Robert Hicks wrote:
> I need to pull data out of Oracle and stuff it into an Excel
> spreadsheet. What modules have you used to interface with Excel and
> would you recommend it?

It is possible to control Excel directly from the Python code (you do
not need to write Excel macros within the Excel). It works flawlessly
for me.

My code goes for example:
import win32api
from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible=0
xlApp.Workbooks.Add()
.
.
=== snip ===

It is helpful to find values of VBA (Visual Basic for Applications)
constants on the Internet or in the Excel documentation and this
constants values assign as Python constants with the same names as in
VBA in the code. For example:

xlToLeft = 1
xlToRight = 2
xlUp = 3
xlDown = 4
xlThick = 4
xlThin = 2
xlEdgeBottom=9

Than you can use exactly the same code as in your Excel macros
(including formating etc.).
=== snip ===
xlApp.Range(xlApp.Selection, xlApp.Selection.End(xlToRight)).Select()
xlApp.Range(xlApp.Selection, xlApp.Selection.End(xlDown)).Select()
xlApp.Selection.NumberFormat = "# ##0"
xlApp.Selection.HorizontalAlignment = xlRight
xlApp.Selection.IndentLevel = 1
=== snip ===

HTH
Petr Jakes




More information about the Python-list mailing list