By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,408 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Dealing with Excel

P: n/a
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?

Robert

Oct 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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

Oct 18 '05 #2

P: n/a
I just want to be and maybe I am not reading your response right. I am
talking about reading in bunch of rows out of Oracle and writing them
to an excel file, not using macros.

Robert

Oct 18 '05 #3

P: n/a
"Robert Hicks" <si*****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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?


What does one use to bind Microsoft libraries to Python?
I think it would be "win32com" and I confess to not having used it.

Best bet would be to use Microsofts ADODB library together with Excels own
CopyFromRecordset function. Using ADODB, you can easily create a connection
to an Oracle server. You would use this to stuff an ADODB.Recordset object
with query results. Once you have your recordset stuffed with query results
you can pass it to the Excel "CopyFromRecordset" function:

Worksheets("Whatever").Cells(1,1).CopyFromRecordse t {recordset object}

and wham! - You have it in a table on a worksheet.

Thomas Bartkus


Oct 18 '05 #4

P: n/a
Hi!

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?


if it is enough to produce a file that excel can read (in contrast to "a
real .xls file"), you could use the csv module:

http://python.org/doc/2.4.2/lib/module-csv.html

Cheers,

Carl Friedrich Bolz

Oct 18 '05 #5

P: n/a
Robert
Sorry I was not more clear in my posting. I am solving similar problem
as you are.

1) I am getting my data from the Firebird SQL database - directly,
using SQL commands (kinterbasdb module), not using ODBC, or ADODB or
what ever - some people here can suggest you how to connect directly to
the Oracle.

2) In the Python code, I am processing data I have got from the
Firebird (I have data stored in the two dimensional list usually)

3) I am setting up the Excel cell range according to the final size of
data using Visual Basic for Applications commands for example:

rng=xlApp.Range(xlApp.Cells(1,1),xlApp.Cells(len(r w),len(rw[0])))

4) I am putting data from the Python to the Excel
rng.Value=rw

5) I am formatting the data in the Excel worksheet using the VBA code
from the Python code and finally I can save it, (it is possible get
Excel under the full control from the Python).

That's it!

I am just a newbie in the Python, so I somebody here can show you
different (better) way to go, but above mentioned works for me great.

If you are looking for the way how to create (generate) the Excel file
directly from the Python, I didn't find it. The only simple way I have
found in this discussion group is to save your data separated by
semicolons in the file with the .csv extension. Excel will recognize it
as an Excel file and open it without problems.

Petr Jakes

Oct 18 '05 #6

P: n/a
No, I have to format fields and everything sad to say. Another poster
up the chain of this posting gave me the nudge in the direction I
needed.

Thanks all,

Robert

Oct 18 '05 #7

P: n/a
>>>>> "Robert" == Robert Hicks <si*****@gmail.com> writes:

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

Robert> Robert

For simple enough tasks, I think you can make SQL*plus output HTML,
which Excel could suck up directly.
For a sripted approach, I would recommend getting an ADODB.Connection
object to your Oracle database
, opening an ADODB.Recordset against the connection
, setting an Excel.Range object to Cell A1
, enumerating your recodset field names into the cells of row 1
, setting the Exel.Range to Cell A2
, using the ridiculously fast CopyFromRecordset method of the
Excel.Range to dump the recordset to the sheet.
HTH,
Chris
Oct 19 '05 #8

P: n/a
Robert Hicks wrote:
No, I have to format fields and everything sad to say. Another poster
up the chain of this posting gave me the nudge in the direction I
needed.


Doesn't Excel also support (in addition to binary .xls and simple text
..csv files) an XML format, which allows full access to formatting and
all other such features? I would assume it's reasonably well documented
and you could just generate that output directly.

-Peter
Oct 19 '05 #9

P: n/a
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?

Robert


http://sourceforge.net/projects/pyexcelerator/
http://sourceforge.net/projects/pyxlwriter/

We use the latter one in the past. As long as
your output is plain enough. It's effective
and MS-free.

The former should be more powerful. Not tested
here.

Hope this helps

Francois
Oct 19 '05 #10

P: n/a
Using ODBC interface to Oracle Excel can do this without
any external (Python) program. Just:

1) Create a ODBC DSN that interfaces with Oracle
2) In Excel do Data-Get External Data-New Database Query
3) Tell Excel what tables/columns/order/filtering you want

You can even save the Database Query for later re-use.
If you want, you can automate this process using Python
COM+ interface to Excel.

Larry Bates

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?

Robert

Oct 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.