Using Python to write to an Excel / OpenOffice Calc spreadsheet on Ubuntu Linux
Via Matt Harrison's blog post, here is how to write Excel or OpenOffice.org Calc spreadsheet files using Python and the xlwt library. Xlwt is a fork of pyExcelerator which handles only writing spreadsheet files. For reading spreadsheets, see xlrd. Note, these libraries don't use COM, so they will work on non-Windows OSes, such as Linux. For more information, see Matt's blog post. He even has a PDF cheat sheet.
- Install pip
- Install xlwt
sudo pip install xlwt
- Create an example script:
import xlwt DATA = (("The Essential Calvin and Hobbes", 1988,), ("The Authoritative Calvin and Hobbes", 1990,), ("The Indispensable Calvin and Hobbes", 1992,), ("Attack of the Deranged Mutant Killer Monster Snow Goons", 1992,), ("The Days Are Just Packed", 1993,), ("Homicidal Psycho Jungle Cat", 1994,), ("There's Treasure Everywhere", 1996,), ("It's a Magical World", 1996,),) wb = xlwt.Workbook() ws = wb.add_sheet("My Sheet") for i, row in enumerate(DATA): for j, col in enumerate(row): ws.write(i, j, col) ws.col(0).width = 256 * max([len(row[0]) for row in DATA]) wb.save("myworkbook.xls")
- Results:
8
Comments
—
Comments feed for this post
#3 Darryl Hebbes commented on 2010-04-07:
and something I struggled to get right, adding frozen headings with styling to the worksheet:
import xlwt
DATA = (("The Essential Calvin and Hobbes", 1988,),
("The Authoritative Calvin and Hobbes", 1990,),
("The Indispensable Calvin and Hobbes", 1992,),
("Attack of the Deranged Mutant Killer Monster Snow Goons", 1992,),
("The Days Are Just Packed", 1993,),
("Homicidal Psycho Jungle Cat", 1994,),
("There's Treasure Everywhere", 1996,),
("It's a Magical World", 1996,),)
wb = xlwt.Workbook()
ws = wb.add_sheet("My Sheet")
# Add headings with styling and froszen first row
heading_xf = xlwt.easyxf('font: bold on; align: wrap on, vert centre, horiz center')
headings = ['Bill Watterson Books', 'Year Published']
rowx = 0
ws.set_panes_frozen(True) # frozen headings instead of split panes
ws.set_horz_split_pos(rowx+1) # in general, freeze after last heading row
ws.set_remove_splits(True) # if user does unfreeze, don't leave a split there
for colx, value in enumerate(headings):
ws.write(rowx, colx, value, heading_xf)
for i, row in enumerate(DATA):
for j, col in enumerate(row):
ws.write(i, j, col)
ws.col(0).width = 256 * max([len(row[0]) for row in DATA])
wb.save("myworkbook.xls")
#5 Eloi commented on 2010-05-04:
In comment #3 where is: ws.write(i, j, col)
Should be: ws.write(i+1, j, col)
Otherwise it will rise: Exception: Attempt to overwrite cell
Thank's Eliot for the post it helped me a lot and thank you Darryl for the cool tip.
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(4)
-
aws
(8)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(9)
-
conkeror
(2)
-
cygwin
(18)
-
datastructures
(15)
-
datetime
(3)
-
dell
(3)
-
django
(39)
-
emacs
(20)
-
files_directories
(10)
-
install_setup
(7)
-
javascript
(3)
-
keyboard
(6)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
preferences
(8)
-
processes
(3)
-
pyqt
(18)
-
python
(122)
-
ratpoison
(3)
-
regexes
(5)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(13)
-
ssh
(7)
-
subversion
(6)
-
twisted
(6)
-
ubuntu
(60)
-
urxvt
(5)
-
vxworks
(25)
-
webservices
(4)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan
#1 Derek commented on 2010-02-25:
Just for clarity: I don't think its actually necessary to install pip in order to install xlwt (tho' it may be easier...).