How can I hide columns in Openpyxl?
Columns can be grouped:
ws.column_dimensions.group(start='B', end='CU', hidden=True)
You can use a loop for a defined workbook wb. in this example I have 10 columns with data and want to hidden all the remaining 16385 is the index of the last excel column, XFD, +1.
import openpyxl as op
worksheet = wb['Sheet1']
max_column =ws.max_column
last_column = op.utils.cell.column_index_from_string('XFD')
for idx in range(max_column+1, last_column+1):
ws.column_dimensions[op.utils.get_column_letter(idx)].hidden = True
if you know the positions of your columns then will be easy
You should set the hidden
attribute to True
:
worksheet.column_dimensions['A'].hidden= True
In order to hide more than one column:
for col in ['A', 'B', 'C']:
worksheet.column_dimensions[col].hidden= True