I am trying to read only non-hidden columns in an excel worksheet and create a dataframe using the same. Working with both pandas and openpyxl.
Openpyxl does not find consecutive hidden columns when using column_dimension. If there is grouping in creating the hidden state, only the first hidden column is returned. For example if columns E and F are hidden as a group then, E has hidden set to true and F is missing from a list of columns. So, what I have done is taken the difference between all possible columns and all cols in the sheet, hence getting the missing hidden columns. It is then concatenated with those having the hidden state to get 'all' hidden columns.
But what is happening is some of the columns which are not in the column_dimensions are not showing as hidden in the actual excel sheet. Not sure how to get a list of the real 'hidden' columns only.
Here is the code I have written
# reading the file and worksheet
wb = load_workbook(path, read_only = False)
ws = wb['Overview']
mx_col = ws.max_column
col_indx =[]
for i in range(1,mx_col+1):
num = get_column_letter(i)
col_indx.append(num)
hid_cols = []
col_vals = []
for col, dimension in ws.column_dimensions.items():
col_vals.append(col)
if dimension.hidden:
hid_cols.append(col)
diff = list(set(col_indx) - set(col_vals))
hidden_columns = diff+hid_cols