Set style

264 views
Skip to first unread message

aklaver

unread,
Feb 11, 2009, 4:28:34 PM2/11/09
to python-excel
I am trying to get the behavior in the row_styles.py example. When I
run that example it works. When I use my simple example below it does
not. What am I missing?

import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet("Style Test")
dt1 = "test"
dt2 = "test2"
style = xlwt.easyxf("font:italic on")
ws.write(0,0,dt1)
ws.write(1,0,dt2)
ws.row(0).set_style(style)
ws.row(1).set_style(style)
wb.save("style_test.xls")

John Machin

unread,
Feb 11, 2009, 6:25:22 PM2/11/09
to python...@googlegroups.com
On 12/02/2009 8:28 AM, aklaver wrote:
> I am trying to get the behavior in the row_styles.py example. When I
> run that example it works.

You must have an extremely interesting definition of "works".
Unfortunately the row_styles.py example is a rather useless example; it
"works" only to the extent that it runs to completion without an
exception, and Excel doesn't throw a tantrum when you open the resultant
file.

What Row.set_style() does is to set the *default* style to be used for
*empty* cells in the row. Here is what the xlrd docs say about default
formatting:

"""
Default Formatting

Default formatting is applied to all empty cells (those not described by
a cell record). Firstly row default information (ROW record, Rowinfo
class) is used if available. Failing that, column default information
(COLINFO record, Colinfo class) is used if available. As a last resort
the worksheet/workbook default cell format will be used; this should
always be present in an Excel file, described by the XF record with the
fixed index 15 (0-based). By default, it uses the worksheet/workbook
default cell style, described by the very first XF record (index 0).
"""

The only behaviour change attempted by the example is to set the font
height. You will notice that the font height in the non-empty cells
("Test") is NOT changed. No change in a font attribute (including of
course italic) will affect empty cells, because there's no content to be
written to the screen for an empty cell, and if you're not writing, you
don't use the font, so it doesn't matter what the font is.

The only visual effect of the example is that the row height is changed.
AFAICT this is caused by Excel going into nanny mode and ensuring that
the row is high enough to support the tallest font mentioned in
connection with the row.

A revised example should vary some attribute whose behaviour change is
visible even with empty cells e.g. the canvas pattern.
Something like this, tarted up a bit, should do the trick:
"""
from xlwt import *
w = Workbook()
ws = w.add_sheet('Row.set_style')
for i in range(6, 80):
style = easyxf("pattern: pattern solid, fore-colour %d" % (i % 32))
ws.write(i, 1, 'Test')
ws.row(i).set_style(style)
w.save('row_styles.xls')
"""

> When I use my simple example below it does
> not. What am I missing?
>
> import xlwt
> wb = xlwt.Workbook()
> ws = wb.add_sheet("Style Test")
> dt1 = "test"
> dt2 = "test2"
> style = xlwt.easyxf("font:italic on")

You said that you were "trying to get the behavior in the row_styles.py
example" but it doesn't attempt to vary the italic attribute ... ??

If you want to use a particular style for all cells in a row, or in all
rows of a particular type, use easyxf to set up the style once, and
specify it in the Worksheet.write() call as/when appropriate.

Otherwise, tell us what you really want to do, and we can help.

@ the rest of y'all: Any volunteers to clean up those "Hey, Dude" examples?

Cheers,
John

Adrian Klaver

unread,
Feb 12, 2009, 10:20:03 AM2/12/09
to python...@googlegroups.com
On Wednesday 11 February 2009 3:25:22 pm John Machin wrote:
> On 12/02/2009 8:28 AM, aklaver wrote:
> > I am trying to get the behavior in the row_styles.py example. When I
> > run that example it works.
>
> You must have an extremely interesting definition of "works".
> Unfortunately the row_styles.py example is a rather useless example; it
> "works" only to the extent that it runs to completion without an
> exception, and Excel doesn't throw a tantrum when you open the resultant
> file.

From the README.html-

"Documentation:

Documentation can be found in the 'doc' directory of the xlwt package. If these
aren't sufficient, please consult the code in the examples directory and the
source code itself. "

Note the suggestion to consult the examples directory. If the examples are bogus
then they should not be there. Lecturing a user who actually bothered to RTFM
and was tripped up by a goes nowhere does nothing example is counter
productive.

>
> What Row.set_style() does is to set the *default* style to be used for
> *empty* cells in the row. Here is what the xlrd docs say about default
> formatting:
>
> """
> Default Formatting
>
> Default formatting is applied to all empty cells (those not described by
> a cell record). Firstly row default information (ROW record, Rowinfo
> class) is used if available. Failing that, column default information
> (COLINFO record, Colinfo class) is used if available. As a last resort
> the worksheet/workbook default cell format will be used; this should
> always be present in an Excel file, described by the XF record with the
> fixed index 15 (0-based). By default, it uses the worksheet/workbook
> default cell style, described by the very first XF record (index 0).
> """
>
> The only behaviour change attempted by the example is to set the font
> height. You will notice that the font height in the non-empty cells
> ("Test") is NOT changed. No change in a font attribute (including of
> course italic) will affect empty cells, because there's no content to be
> written to the screen for an empty cell, and if you're not writing, you
> don't use the font, so it doesn't matter what the font is.
>
> The only visual effect of the example is that the row height is changed.
> AFAICT this is caused by Excel going into nanny mode and ensuring that
> the row is high enough to support the tallest font mentioned in
> connection with the row.

In the above README it might be good to mention that there is more complete
documentation in the xlrd package.

>
> A revised example should vary some attribute whose behaviour change is
> visible even with empty cells e.g. the canvas pattern.
> Something like this, tarted up a bit, should do the trick:
> """
> from xlwt import *
> w = Workbook()
> ws = w.add_sheet('Row.set_style')
> for i in range(6, 80):
> style = easyxf("pattern: pattern solid, fore-colour %d" % (i % 32))
> ws.write(i, 1, 'Test')
> ws.row(i).set_style(style)
> w.save('row_styles.xls')
> """
>
> > When I use my simple example below it does
> > not. What am I missing?
> >
> > import xlwt
> > wb = xlwt.Workbook()
> > ws = wb.add_sheet("Style Test")
> > dt1 = "test"
> > dt2 = "test2"
> > style = xlwt.easyxf("font:italic on")
>
> You said that you were "trying to get the behavior in the row_styles.py
> example" but it doesn't attempt to vary the italic attribute ... ??

The behavior being setting a style a row at a time instead of cell by cell.

>
> If you want to use a particular style for all cells in a row, or in all
> rows of a particular type, use easyxf to set up the style once, and
> specify it in the Worksheet.write() call as/when appropriate.
>
> Otherwise, tell us what you really want to do, and we can help.

Seemed self evident, set the style for a row with one call instead of iterating
over cells. This is possible in a live spreadsheet by selecting an entire row
or column and applying a format. Seemed natural one could do it within the
context of the program. Is not the case at it turns out.

>
> @ the rest of y'all: Any volunteers to clean up those "Hey, Dude" examples?
>
> Cheers,
> John
>
>
>


--
Adrian Klaver
akl...@comcast.net

John Machin

unread,
Feb 12, 2009, 2:41:22 PM2/12/09
to python...@googlegroups.com
On 13/02/2009 2:20 AM, Adrian Klaver wrote:
> On Wednesday 11 February 2009 3:25:22 pm John Machin wrote:
>> On 12/02/2009 8:28 AM, aklaver wrote:
>>> I am trying to get the behavior in the row_styles.py example. When I
>>> run that example it works.
>> You must have an extremely interesting definition of "works".
>> Unfortunately the row_styles.py example is a rather useless example; it
>> "works" only to the extent that it runs to completion without an
>> exception, and Excel doesn't throw a tantrum when you open the resultant
>> file.
>
>>From the README.html-
>
> "Documentation:
>
> Documentation can be found in the 'doc' directory of the xlwt package. If these
> aren't sufficient, please consult the code in the examples directory and the
> source code itself. "
>
> Note the suggestion to consult the examples directory. If the examples are bogus
> then they should not be there.

Agreed. As you'll notice from my message, I've suggested a better
example and also called for volunteers for better examples.

> Lecturing a user who actually bothered to RTFM
> and was tripped up by a goes nowhere does nothing example is counter
> productive.

Which part of my message do you regard as lecturing you?


>> What Row.set_style() does is to set the *default* style to be used for
>> *empty* cells in the row. Here is what the xlrd docs say about default
>> formatting:
>>
>> """
>> Default Formatting
>>
>> Default formatting is applied to all empty cells (those not described by
>> a cell record). Firstly row default information (ROW record, Rowinfo
>> class) is used if available. Failing that, column default information
>> (COLINFO record, Colinfo class) is used if available. As a last resort
>> the worksheet/workbook default cell format will be used; this should
>> always be present in an Excel file, described by the XF record with the
>> fixed index 15 (0-based). By default, it uses the worksheet/workbook
>> default cell style, described by the very first XF record (index 0).
>> """
>>
>> The only behaviour change attempted by the example is to set the font
>> height. You will notice that the font height in the non-empty cells
>> ("Test") is NOT changed. No change in a font attribute (including of
>> course italic) will affect empty cells, because there's no content to be
>> written to the screen for an empty cell, and if you're not writing, you
>> don't use the font, so it doesn't matter what the font is.
>>
>> The only visual effect of the example is that the row height is changed.
>> AFAICT this is caused by Excel going into nanny mode and ensuring that
>> the row is high enough to support the tallest font mentioned in
>> connection with the row.
>
> In the above README it might be good to mention that there is more complete
> documentation in the xlrd package.

Agreed.

>> A revised example should vary some attribute whose behaviour change is
>> visible even with empty cells e.g. the canvas pattern.
>> Something like this, tarted up a bit, should do the trick:
>> """
>> from xlwt import *
>> w = Workbook()
>> ws = w.add_sheet('Row.set_style')
>> for i in range(6, 80):
>> style = easyxf("pattern: pattern solid, fore-colour %d" % (i % 32))
>> ws.write(i, 1, 'Test')
>> ws.row(i).set_style(style)
>> w.save('row_styles.xls')
>> """
>>
>>> When I use my simple example below it does
>>> not. What am I missing?
>>>
>>> import xlwt
>>> wb = xlwt.Workbook()
>>> ws = wb.add_sheet("Style Test")
>>> dt1 = "test"
>>> dt2 = "test2"
>>> style = xlwt.easyxf("font:italic on")
>> You said that you were "trying to get the behavior in the row_styles.py
>> example" but it doesn't attempt to vary the italic attribute ... ??
>
> The behavior being setting a style a row at a time instead of cell by cell.

But that *isn't* its behaviour.


>> If you want to use a particular style for all cells in a row, or in all
>> rows of a particular type, use easyxf to set up the style once, and
>> specify it in the Worksheet.write() call as/when appropriate.
>>
>> Otherwise, tell us what you really want to do, and we can help.
>
> Seemed self evident, set the style for a row with one call instead of iterating
> over cells.

I had guessed this as a possibility ... see "If you want to use a
particular style for all cells in a row" above. Please bear in mind that
what is "evident" to one person may not be so to another.

> This is possible in a live spreadsheet by selecting an entire row
> or column and applying a format. Seemed natural one could do it within the
> context of the program. Is not the case at it turns out.

>> @ the rest of y'all: Any volunteers to clean up those "Hey, Dude" examples?

Hmmm ... c'mon y'all!

Reply all
Reply to author
Forward
0 new messages