Reputation: 2717
I want to change all columns width of multiple sheets in my excel file. What I found in other posts was about changing the width of one column. How can I do this?
This is what I tried but failed:
wb = openpyxl.load_workbook('my_file.xlsx')
for sheet in wb.worksheets:
for column in sheet.columns:
sheet.column_dimensions[column].width = 50
Upvotes: 0
Views: 1220
Reputation: 1830
Hi you can use the following code this one build for dynamic cell resize but can use for your purpose also
column_widths = []
for row in data:
for i, cell in enumerate(row):
if len(column_widths) > i:
if len(cell) > column_widths[i]:
column_widths[i] = 50
else:
column_widths += [50]
for i, column_width in enumerate(column_widths):
worksheet.column_dimensions[get_column_letter(i+1)].width = column_width
Upvotes: 1