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
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!