Putting borders around a large group of cells

1,172 views
Skip to first unread message

WaxingFervor

unread,
Apr 13, 2011, 4:28:26 PM4/13/11
to python-excel
I would like to outline a block of cells, say 10 rows tall by 20
columns wide.

I see in the tutorial python-excel.pdf (author: thanks for that btw)
on page 42 that there is a little description of how to do this. It
says:

Writing a single cell with borders is simple enough, however applying
a border to a group
of cells is painful as shown in this example:

from xlwt import Workbook,easyxf
tl = easyxf('border: left thick, top thick')
t = easyxf('border: top thick')
tr = easyxf('border: right thick, top thick')
r = easyxf('border: right thick')
br = easyxf('border: right thick, bottom thick')
b = easyxf('border: bottom thick')
bl = easyxf('border: left thick, bottom thick')
l = easyxf('border: left thick')

w = Workbook()
ws = w.add_sheet('Border')
ws.write(1,1,style=tl)
ws.write(1,2,style=t)
ws.write(1,3,style=tr)
ws.write(2,3,style=r)
ws.write(3,3,style=br)
ws.write(3,2,style=b)
ws.write(3,1,style=bl)
ws.write(2,1,style=l)

Is there an easier way to do this? In VB I would assign that block of
cells to an object and then tell Excel to put an outer border on.

John Machin

unread,
Apr 14, 2011, 8:08:44 PM4/14/11
to python...@googlegroups.com


On Thursday, April 14, 2011 6:28:26 AM UTC+10, WaxingFervor wrote:
I would like to outline a block of cells, say 10 rows tall by 20
columns wide.

I see in the tutorial python-excel.pdf (author: thanks for that btw)
on page 42 that there is a little description of how to do this.  It
says:

Writing a single cell with borders is simple enough, however applying
a border to a group
of cells is painful as shown in this example:

[snip]


Is there an easier way to do this?  In VB I would assign that block of
cells to an object and then tell Excel to put an outer border on.

Unfortunately not. The internal data model of pyExcelerator/xlwt is purposed towards writing data plus formatting at the same time, making it very difficult to iterate over a collection of existing cells and change only a few border attributes.

WaxingFervor

unread,
Apr 25, 2011, 11:44:45 AM4/25/11
to python-excel
OK, understood. Now a follow-up question....

Is there any way to write a cell, and then later format that cell?
Can these write and format steps be separated, or does xlwt always
need to write in order to format?
> few border attributes.- Hide quoted text -
>
> - Show quoted text -

John Machin

unread,
Apr 29, 2011, 3:32:41 PM4/29/11
to python...@googlegroups.com
On Tuesday, 26 April 2011 01:44:45 UTC+10, WaxingFervor wrote:
OK, understood.  Now a follow-up question....

Is there any way to write a cell, and then later format that cell?
Can these write and format steps be separated, or does xlwt always
need to write in order to format?

A cell will always be created with a style, even if it is only the default style.

Enhancements that allow
(a) changing the value while retaining the existing style
(b) changing the style while retaining the existing value
are quite high on the priority list.

Cheers,
John
Reply all
Reply to author
Forward
0 new messages