Basic read/write question

79 views
Skip to first unread message

Rui

unread,
May 7, 2009, 11:02:12 AM5/7/09
to python-excel
Hi all

I'm starting to use xlrd/xlwt/xlutils to read/write excel sheets but
I'm having some problems getting my head around the way all of this
should be used.

My needs are (for the moment quite basic):
-- read excel sheet (with formating)
-- modify specific cells (that match criteria based on other cells of
the same line)
-- write the excel sheet with exactly the same formatting.

I've been successful in reading, modifying and writing the modified
workbook but without any formatting.
With formatting, however, I have been unable to do so ... I cannot
manage to get it to work ...

I've tried reading all the examples and docs but can't get a clear
picture of what I need to do.

Would a kind sould point me towards the light by giving me a bullet
point list of what needs to happen (classes that need to be sub-
classed, methods that need to be redefined, etc)

Thanks A LOT in advance

Rui

Chris Withers

unread,
May 8, 2009, 5:37:17 AM5/8/09
to python...@googlegroups.com
Rui wrote:
> -- modify specific cells (that match criteria based on other cells of
> the same line)

It depends exactly what you mean by this as to whether I'd recommend
xlutils.copy or xlutils.filter.
Can you give some example code for the modifications you wish to make?

Both xlutils.copy and xlutils.filter have docs in the docs folder of the
source distribution of xlutils. Perhaps you could have a read of those
and let me know what doesn't make sense?

cheers,

Chris

--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk

Rui

unread,
May 10, 2009, 5:18:39 PM5/10/09
to python-excel
I want to modify the contents of specifc cells (just modify the text)
but keep everything else: cell formatting, conditional formatting,
etc...

I'm having trouble with the specifics of using a global reader, a
global writer when using the filter module or keeping the formatting
when just using copy ...

Rui

Chris Withers

unread,
May 10, 2009, 5:43:30 PM5/10/09
to python...@googlegroups.com
Rui wrote:
> I want to modify the contents of specifc cells (just modify the text)
> but keep everything else: cell formatting, conditional formatting,
> etc...

- it's hard when using xlutils.copy to keep formatting for an existing
cell whose value you want to modify.

- neither xlwt nor xlrd support conditional formatting, so xlutils can't
help you there either.

> I'm having trouble with the specifics of using a global reader, a
> global writer when using the filter module or keeping the formatting
> when just using copy ...

Dunno what you mean by global reader or global writer.

I'd probably do something like:

from xlrd import open_workbook
from xlwt import easyxf
from xlutils.copy import copy

# customise this to your needs, have more than one style
# if you need it
mystyle = easyxf('font:color red;')

rb = open_workbook('source.xls',formatting_info=True)
wb = copy(rb)

# do whatever modifications you need
wb.get_sheet(0).write(10,10,'some value',mystyle)

wb.save('output.xls')

Yes, it's not keeping the original formatting of the modified cells, but
it's better than nothing ;-)

Reply all
Reply to author
Forward
0 new messages