Can't set style for row in 2.1

590 views
Skip to first unread message

shawn....@gmail.com

unread,
Oct 2, 2014, 2:36:55 PM10/2/14
to openpyx...@googlegroups.com
I've tried this several ways, and get errors every way I try. I want to set a Style for an entire row. Documentation says to do it like so:

row = ws.row_dimensions[1]
row.style = Style()

When I do this, I get a "'NoneType' object has no attribute 'parent'" error at 
 File "C:\Virtual\Dj17_OPXL21\lib\site-packages\openpyxl\worksheet\dimensions.py", line 52, in style
   
self._style = self.worksheet.parent.shared_styles.add(style)


My specific code is as follows:
   self.column_header_style = Style(font=self.bold_11, alignment=self.centered, border=self.top_bottom_thick_border)
...

   row
= self.worksheet.row_dimensions[4]
   row
.style = self.column_header_style

I also tried to do it directly on the RowDimension object that I use to add the row to the worksheet, like so:


rd = RowDimension(index=4)
rd.style = self.column_header_style
self.worksheet.row_dimensions[4] = rd

I get the exact same error both times. Is this a bug, or am I doing something wrong. Please help!


Charlie Clark

unread,
Oct 2, 2014, 3:14:25 PM10/2/14
to openpyx...@googlegroups.com, shawn....@gmail.com
Hiya,

Am .10.2014, 20:36 Uhr, schrieb <shawn....@gmail.com>:

> I've tried this several ways, and get errors every way I try. I want to
> set
> a Style for an entire row. Documentation says to do it like so:
> row = ws.row_dimensions[1]
> row.style = Style()

That should work without a problem. At least it does here and when the
documentation is checked.

> When I do this, I get a "'NoneType' object has no attribute 'parent'"
> error
> at
> File
> "C:\Virtual\Dj17_OPXL21\lib\site-packages\openpyxl\worksheet\dimensions.py",
> line 52, in style
> self._style = self.worksheet.parent.shared_styles.add(style)

This tells us that self.worksheet is None. Are you working with an
existing file or creating a new one? RowDimensions are created only when
new rows are (implicitly) added but you should get an IndexError

> My specific code is as follows:
> self.column_header_style = Style(font=self.bold_11, alignment=self.
> centered, border=self.top_bottom_thick_border)
> ...
> row = self.worksheet.row_dimensions[4]
> row.style = self.column_header_style

What is "self" here?

> I also tried to do it directly on the RowDimension object that I use to
> add
> the row to the worksheet, like so:
> rd = RowDimension(index=4)
> rd.style = self.column_header_style
> self.worksheet.row_dimensions[4] = rd

That will fail because you need to pass in a worksheet to the RowDimension

> I get the exact same error both times. Is this a bug, or am I doing
> something wrong. Please help!

Can you paste the whole section of code related to creating this row?

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

shawn....@gmail.com

unread,
Oct 2, 2014, 3:36:29 PM10/2/14
to openpyx...@googlegroups.com, shawn....@gmail.com


On Thursday, October 2, 2014 2:14:25 PM UTC-5, Charlie Clark wrote:
Hiya,

Am .10.2014, 20:36 Uhr, schrieb <shawn....@gmail.com>:

> I've tried this several ways, and get errors every way I try. I want to  
> set
> a Style for an entire row. Documentation says to do it like so:
> row = ws.row_dimensions[1]
> row.style = Style()

That should work without a problem. At least it does here and when the  
documentation is checked.

> When I do this, I get a "'NoneType' object has no attribute 'parent'"  
> error
> at
>  File
> "C:\Virtual\Dj17_OPXL21\lib\site-packages\openpyxl\worksheet\dimensions.py",
> line 52, in style
>     self._style = self.worksheet.parent.shared_styles.add(style)

This tells us that self.worksheet is None. Are you working with an  
existing file or creating a new one? RowDimensions are created only when  
new rows are (implicitly) added but you should get an IndexError

>>>>>>>>>>>>>>>
That's what I thought, but that doesn't make any sense, as just before this call I'm merging cells in the worksheet, getting that merged cell, and setting both a style and a value for the cell. I'm creating a new file.

 

> My specific code is as follows:
>    self.column_header_style = Style(font=self.bold_11, alignment=self.
> centered, border=self.top_bottom_thick_border)
> ...
>   row = self.worksheet.row_dimensions[4]
>    row.style = self.column_header_style

What is "self" here?

>>>>>>>>>>>>
self is my report class that I've built, so it has a reference to the workbook, the active worksheet for that workbook, and the fonts, borders, styles, etc. that are used throughout the class methods 

> I also tried to do it directly on the RowDimension object that I use to  
> add
> the row to the worksheet, like so:
> rd = RowDimension(index=4)
> rd.style = self.column_header_style
> self.worksheet.row_dimensions[4] = rd

That will fail because you need to pass in a worksheet to the RowDimension

> I get the exact same error both times. Is this a bug, or am I doing
> something wrong. Please help!

Can you paste the whole section of code related to creating this row?

>>>>>>>>>>>>>>>>>>>>>>

I've posted several rows being created, as the first one works properly (though I'm setting the style on the cell in the first row) while the second fails:

#set the row heights for the header row
rd = RowDimension(index=2, height=23.25)
self.worksheet.row_dimensions[2] = rd
#merge the title cells
self.worksheet.merge_cells('A2:E2')

#add the text for the title row
title = self.worksheet.cell('A2')
title.style = self.zrt_header_style
title.value = r'Review Team Meeting - 9 am - ' + self.zrt_date.strftime('%A %d, %Y')
#add the column header row
rd = RowDimension(index=4)
rd.style = self.column_header_style
self.worksheet.row_dimensions[4] = rd

#add the cell values for the column header row
time_head = self.worksheet.cell('A4')
time_head.value = r'Time'

 

Charlie Clark

unread,
Oct 2, 2014, 3:40:44 PM10/2/14
to openpyx...@googlegroups.com
Am .10.2014, 21:36 Uhr, schrieb <shawn....@gmail.com>:

> I've posted several rows being created, as the first one works properly
> (though I'm setting the style on the cell in the first row) while the
> second fails:
> #set the row heights for the header row
> rd = RowDimension(index=2, height=23.25) # worksheet=None

You must pass in the worksheet! But in general I'd avoid creating
RowDimensions directly.

> self.worksheet.row_dimensions[2] = rd
> #merge the title cells
> self.worksheet.merge_cells('A2:E2')
> #add the text for the title row
> title = self.worksheet.cell('A2')
> title.style = self.zrt_header_style
> title.value = r'Review Team Meeting - 9 am - ' +
> self.zrt_date.strftime('%A
> %d, %Y')
# use string formatting methods. Much nicer and you only need raw strings
when you don't want to escape with "\" such as when you're writing regexes
or file paths in Windows.

> #add the column header row
> rd = RowDimension(index=4) # worksheet=None

> rd.style = self.column_header_style
> self.worksheet.row_dimensions[4] = rd
> #add the cell values for the column header row
> time_head = self.worksheet.cell('A4')
> time_head.value = r'Time'

shawn....@gmail.com

unread,
Oct 2, 2014, 4:25:03 PM10/2/14
to openpyx...@googlegroups.com
Got it. Passing in the worksheet resolves the issue, but doesn't behave as I expect, as setting the row style doesn't override the style of each cell that a value has been placed in. It seems the best way is to set the style directly for each cell I touch.  Thanks for your help, and thanks for the string formatting suggestion.

Charlie Clark

unread,
Oct 2, 2014, 5:25:05 PM10/2/14
to openpyx...@googlegroups.com
Am .10.2014, 22:25 Uhr, schrieb <shawn....@gmail.com>:

> Got it. Passing in the worksheet resolves the issue, but doesn't behave
> as
> I expect, as setting the row style doesn't override the style of each
> cell
> that a value has been placed in. It seems the best way is to set the
> style
> directly for each cell I touch. Thanks for your help, and thanks for the
> string formatting suggestion.

The specification doesn't say very much about precedence but I would
expect cell formatting to override row and column formatting. If you want
to apply the same style to lots of cells you might find the recent
discussion useful:

https://bitbucket.org/openpyxl/openpyxl/issue/365/
Reply all
Reply to author
Forward
0 new messages