how to check if row is hidden

3,415 views
Skip to first unread message

robert rottermann

unread,
Jan 28, 2014, 1:24:44 PM1/28/14
to openpyx...@googlegroups.com
Hi there,
I just started to use openpyxl.
Now I try to check whether a row is hidden but could not find a way to do so.

I would appreciate any help.

thanks
robert

Charlie Clark

unread,
Jan 28, 2014, 4:24:53 PM1/28/14
to openpyx...@googlegroups.com
Am .01.2014, 19:24 Uhr, schrieb robert rottermann <rob...@redcor.ch>:

> Hi there,
> I just started to use openpyxl.
> Now I try to check whether a row is hidden but could not find a way to do
> so.

Hiya Robert,

not really much of an expert on styles but:

ws.row_dimensions[1].visible # where r is the row number (in Excel's
numbers)

should be false on a hidden 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

robert rottermann

unread,
Jan 29, 2014, 6:30:43 AM1/29/14
to openpyx...@googlegroups.com

Thanks Charlie,

this is what I expected, but unfortunately I get the same status (False) for all of them
Also the heigth does not help. I is -1 for nearly all of the rows.

robert

Charlie Clark

unread,
Jan 29, 2014, 6:32:10 AM1/29/14
to openpyx...@googlegroups.com
Am .01.2014, 12:30 Uhr, schrieb robert rottermann <rob...@redcor.ch>:

> Thanks Charlie,
> this is what I expected, but unfortunately I get the same status (False)
> for all of them
> Also the heigth does not help. I is -1 for nearly all of the rows.

I think there is at least one bug related to this. Could you check and if
not submit a new one?

Adam Morris

unread,
Jan 29, 2014, 10:04:01 AM1/29/14
to openpyx...@googlegroups.com
Yes - the issue does exist.  Writing hidden rows works, but the error is that it is not read correctly when opening file.  The bugs are:

https://bitbucket.org/ericgazoni/openpyxl/issue/128/unable-to-hide-rows-using-row_dimensions


Charlie - I'll add something like this as a test

          # Reading row height works, but not reading row hidden / visible status:
from openpyxl import load_workbook
wb = load_workbook('/Users/amorris/Desktop/w1.xlsx')
ws = wb.active
assert ws.row_dimensions[2].visible = True    # incorrect
assert ws.row_dimensions[1].height = -1        # equals -1, because not set in file
assert ws.row_dimensions[3].height  = 30.1    # equals 30.1 as in file
wb.save('/Users/amorris/Desktop/w1-s.xlsx')   #  row 2 no longer visible

# Manually changing row / column visibility does work, and you use it like:
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = 1
ws['A2'] = 2
ws['A3'] = 3
ws['B1'] = 'B'
ws['C1'] = 'C'
ws['D1'] = 'D'
ws.column_dimensions['C'].visible = False
ws.row_dimensions[2].visible = False
assert ws.row_dimensions[3].height == -1      # equals -1 until set
ws.row_dimensions[3].height = 30.1
wb.save('w2.xlsx')                                        # column C is hidden, row 2 is hidden, row 3 is 30.1 high
w1.xlsx

Adam Morris

unread,
Jan 29, 2014, 10:05:17 AM1/29/14
to openpyx...@googlegroups.com
Also - the row height is correct.  -1 means that it has not been set, and is at it's default (i.e. excel has not recorded a row height).

--Adam

Charlie Clark

unread,
Jan 29, 2014, 10:08:55 AM1/29/14
to openpyx...@googlegroups.com
Am .01.2014, 16:05 Uhr, schrieb Adam Morris <w...@myemptybucket.com>:

> Also - the row height is correct. -1 means that it has not been set, and
> is at it's default (i.e. excel has not recorded a row height).

Hi Adam,

thanks very much for the notes. "-1" as unset is a dreadful convention! We
should look at using None instead. I'm not sure if it's related but we
always write out column widths and fail to take advantage of a default
(there is one in units but it must be wrong).

Adam Morris

unread,
Jan 29, 2014, 1:28:06 PM1/29/14
to openpyx...@googlegroups.com
I agree - None makes more sense, and I can't see any reason not to change it.  Width is the same.

The code doesn't actually let you write a width or a height of 0, even though this is valid in excel.

I noticed at some point that excel sometimes saves a default column style that starts at col min="1" and ends at max="16384" - which openpyxl responds by writing a column definition for all 16384 columns (which while valid, makes for a unnecessarily large excel file).  Not sure if that's still the case, but worth investigating.

I'll add a bug report - and address these.
Reply all
Reply to author
Forward
0 new messages