Finding hidden cells with openpyxl

I'm trying to write a script to copy formatting from one book to another, and as anyone who owns openpyxl knows, it's a big script. I got it to work pretty well, but one thing I can't figure out is how to read from the original if the columns are hidden.

Can anyone tell me where to look in a workbook, worksheet, column or cell object to see where the hidden columns are?

+3


source to share


2 answers


Worksheets contain objects row_dimensions

and column_dimensions

that contain information about specific rows or columns, for example, whether they are hidden or not. Column sizes can also be grouped, so you need to keep that in mind when browsing.



+2


source


The attributes you are looking for are inside attributes column_dimensions

and row_dimensions

object Worksheet

.

These are related dictionaries whose values ​​are ColumnDimension

/ RowDimension

objects. The specific attribute you are looking for is ColumnDimension.hidden

.

The following will print the column letter of all hidden columns on the sheet ws

:

for colLetter,colDimension in ws.column_dimensions.items(): if colDimension.hidden == True: print(colLetter)



And for the lines:

for rowNum,rowDimension in ws.row_dimensions.items(): if rowDimension.hidden == True: print(rowNum)

As I understand it, uploading your book as quality read_only

might mess with ws.row_dimensions

, so be careful in this case.

0


source







All Articles