numpy array bridge

175 views
Skip to first unread message

Timmie

unread,
Sep 22, 2009, 7:22:06 AM9/22/09
to python-excel
Hello,
does xlwt have a functionality to read a numpy array into a Excel
table?
And, if it was a structured array, could it also recognise data types
such as dates?

I found this topic:
New member: interested in writing Python bioinformatic scripts, to be
fed from and to an Excel spreadsheet
http://groups.google.com/group/python-excel/browse_thread/thread/82f0de4634b96e35/623a8dede0340542

Didn't quite solve the problem.

Thanks in advance,
Timmie

Kevin Dunn

unread,
Sep 22, 2009, 8:38:38 AM9/22/09
to python...@googlegroups.com
Hi Timmie,

The general approach to create an Excel file is:
w = Workbook()
ws = w.add_sheet('My sheet name')
ws.write(row, cell, <item>)

So you just iterate over all the Numpy array entries, writing them one
at a time to the Excel file. I posted an example previously and the
code is here: http://pastebin.com/f5ea0b737  (use the bottom half of
the page to copy and paste from). This also handles arrays of more
than 65536 rows or 256 columns by creating additional Excel sheets;
basically it just tiles the Numpy array.

Regarding dates: take a look at the example here:
https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/examples/dates.py
Now the syntax just adds an extra parameter: ws.write(row, cell,
<item>, <style>). See the example for what <item> and <style> are.

Kevin

John Machin

unread,
Sep 22, 2009, 9:24:00 AM9/22/09
to python...@googlegroups.com
On 22/09/2009 9:22 PM, Timmie wrote:
> Hello,

Hi Timmie,

> does xlwt have a functionality to read a numpy array into a Excel
> table?

No, and it won't, on the principle that it is better to have M readers +
N writers than M * N copiers. The "for" statement is your friend :-)

Cheers,
John


Timmie

unread,
Sep 23, 2009, 4:25:32 AM9/23/09
to python-excel
> So you just iterate over all the Numpy array entries, writing them one
> at a time to the Excel file. I posted an example previously and the
> code is here:http://pastebin.com/f5ea0b737 (use the bottom half of
> the page to copy and paste from). This also handles arrays of more
> than 65536 rows or 256 columns by creating additional Excel sheets;
> basically it just tiles the Numpy array.
This is a great start which I could recommend everyone willing to
write xl from numpy.
Thanks!

In the post
http://groups.google.com/group/python-excel/msg/9988d0a05cef32ea

I found a link to:

http://code.google.com/p/pyinex/

This has a example for numpy integration in the downloadable archive.

=> WhiIt xlwt is good for writing your numbers to Excel, with pyinex
you may have numpy and all other python libraries calculate based on
Excel input. This also opens the way for graphs dyanically created and
changed in Excel which can be based on numpy results.
I admit that matplotlib graphs are nicer, but many need the Office
files for interaction with other workflows.

Thanks for the good hints,
Timmie
Reply all
Reply to author
Forward
0 new messages