Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

python - Finding hidden cells using openpyxl

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 pretty well, but one thing I can't seem to figure out is how to read from the original if columns are hidden.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...