Finding hidden cells using openpyxl

后端 未结 2 1633
死守一世寂寞
死守一世寂寞 2021-01-06 20:59

I\'ve been trying to write a script to copy formatting from one workbook to another and, as anyone dealing with openpyxl knows, it\'s a big script. I\'ve gotten it to work

2条回答
  •  -上瘾入骨i
    2021-01-06 21:54

    The attributes you are looking for are inside the column_dimensions and row_dimensions attributes of the Worksheet object.

    These are bound dictionaries whose values are ColumnDimension/RowDimension objects. The specific attribute you're looking for is ColumnDimension.hidden.

    The following will print the column letter of all hidden columns in worksheet ws:

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

    And for rows:

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

    As I understand it, loading your workbook as read_only can mess with ws.row_dimensions, so be careful in this case.

提交回复
热议问题