How to get the name of all columns using openpyxl?

Viewed 104

I want to set the width of all columns in excel sheet. I can set the specific column width by

ws.column_dimensions['A'].width = 2.5

I tried to implement in this way.

cell_range = ['A','B','C','D','E','F','G','H','I','J']

for col_names in cell_range:
   ws.column_dimensions[col_names].width = 2.5

How can I iterate through all the name of columns?

1 Answers

I don't have a good way of testing it, but would this work?

from openpyxl.utils import get_column_letter

for col in ws.iter_cols():
    name = get_column_letter(col[0])
    ws.column_dimensions[name].width = 2.5
Related