About

Welcome to Panela, Matt Harrison's take on mostly Open Source, Linux, Python, innovation in those areas, other buzzwords and Dick Proenneke. It comes complete with the illustrations as needed. Note the opinions expressed here are merely my opinions and not the opinions of my employer.

about Matt

Calendar

««Feb 2010»»
SMTWTFS
 
1
23456
78910111213
14151617181920
21222324252627
28

Mailing List

My RSS Feeds








pyExcelerator (xlwt) cheatsheet (create native Excel from pure python)

posted 2008.04.26 Sat

If you are looking to generate/create/write (but not read) excel spreadsheets from native python (read on linux or macos or even Windows!), then xlwt (a fork of pyExcelerator) is your friend. This library, (and the ancestor) is somewhat unknown, but quite good. The only real problem with using it is that documentation (and the bug I mention below), (though this blog post is pretty good) is somewhat spotty. But you can usually get around dearth of docs by reading the code that reveals that the internal model for .xls documents is quite good, and that is supports most features (sans graphing). The original MIA author did quite a good job.

I've made a cheatsheet(pdf) for these tools. Inkscape still has some issues exporting this but here's the source for it (and the formattings bad, but I thought I'd just get it out there). It's an executable cheatsheet, so if you run it (the .py not the .pdf), it will create a spreadsheet. Hopefully this is useful to some folks. I've also tried to embed docs into the code that will serve as useful hints.

Also, I'm using a factory to reuse styles and fonts, as it seems that if you define too many of them, Excel has problems and crashes (note that OOo won't manifest them and happily plugs along on the non-conforming files). Please send any feedback or comments on how this could be more useful.

Also note, this is the .xls format, not the newer xml/ISO/.xlsx stuff

tags:        

links: digg this    del.icio.us    reddit




1. Sergio left...
2008.04.27 Sun 5:35 am

Good post. Thanks for the cheatsheet!


2. Justin left...
2008.04.27 Sun 6:19 pm

Interesting; I'd wanted to use pyExcelerator in the past but was stymied by the requirement of Python 2.5. This fork sounds promising for folks like me stuck with 2.3.4.


3. Matt left...
2008.04.27 Sun 6:52 pm

Justin- PyEx works with 2.4 (I know cause that's what I'm running). xlwt removed the decorators so it works with 2.3....


4. murray left...
2008.05.09 Fri 7:41 pm

thank you!

an 'xlrd' package (Excel read) is described at and available from http://www.lexicon.net/sjmachin/xlrd.htm

so far, both packages appear to work well!

both packages seem to work well.


5. sur left...
2008.07.14 Mon 9:48 am

Thank you so much for your cheat sheet and all the info, it really helped me :)


6. Andreas left...
2008.10.15 Wed 7:57 am

Hi, thanks very much for the cheatsheet!

Any idea how to change the text rotation, so that the text orientation is rotated by 90 degrees? I tried the following, but no luck:

header_style = {"font": (("bold", True),('height', 210)), \ "alignment":(("orie", pycel.Alignment.ORIENTATION_90_CC),)} write(ws, 0, 0, 'My Vertical heading', header_style)

Any help is appreciated, thanks again!

cheers

Andreas


7. John Machin left...
2008.10.21 Tue 9:33 pm

Try out xlwt 0.7.0 from http://p ypi.python.org/pypi/xlwt -- has new facility for creating XFs aka styles. Andreas' example becomes: header_style = xlwt.easyxf("font: bold on, height 210; align: rota 90") # "orie" is BIFF 5, not BIFF8; will be removed from xlwt; use rota instead orie on BOTH packages in ALL contexts (old-fashioned attribute poking, Matt's gadget, easyxf). Discussion at http://groups.google.com/ group/python-excel


8. Axel left...
2008.12.11 Thu 8:59 am

This cheatcheet is indeed very helpful. Many thanks. But what is actually the main difference between xlwt and pyExcelerator ? (why is xlwt "better"?). I was using pyExcelerator and was quite happy with it.


9. jloyzaga left...
2008.12.11 Thu 11:19 pm

I just can't get the cheatsheet.py to work can you send me the .py?


10. John Machin left...
2009.01.27 Tue 2:03 am

@Axel: xlwt is maintained. Check out the python-excel group at http://groups.google.com/group/python-excel


11. Georg left...
2009.06.13 Sat 6:04 am

Great cheatsheet! Maybe you want to include the flush_row_data() trick for saving memory for large amounts of data from your brother. This was also very important for me. Thanks, Georg