1from openpyxl.utils import get_column_letter
2
3column_widths = []
4for row in Workbook:
5 for i, cell in enumerate(row):
6 if len(column_widths) > i:
7 if len(cell) > column_widths[i]:
8 column_widths[i] = len(cell)
9 else:
10 column_widths += [len(cell)]
11
12for i, column_width in enumerate(column_widths):
13 worksheet.column_dimensions[get_column_letter(i+1)].width = column_width
14