Python - Automatically adjust width of an excel file's columns
<p>Newbie - I have a Python script that adjusts the width of different columns of an excel file, according to the values specified:</p>

<pre><code>import openpyxl
from string import ascii_uppercase

newFile = "D:\Excel Files\abc.xlsx"

wb = openpyxl.load_workbook(filename = newFile)        
worksheet = wb.active

for column in ascii_uppercase:
    if (column=='A'):
        worksheet.column_dimensions[column].width = 30
    elif (column=='B'):
        worksheet.column_dimensions[column].width = 40            
    elif (column=='G'):
        worksheet.column_dimensions[column].width = 45            
    else:
        worksheet.column_dimensions[column].width = 15

wb.save(newFile)
</code></pre>

<p>Is there any way through which we can adjust the width of every column to its most optimum value, without explicitly specifying it for different columns (means, without using this "<strong>if-elif-elif-......-elif-else</strong>" structure)? 
Thanks!</p>

<p>If possible you should determine the length of the longest entry in the column and use that to set the width.</p>

<p>I'm assuming you can make use of for entry in ascii_uppercase.</p>

<p>I'm on mobile atm so can't give a concrete code example but what I said before should help you get closer to what you want to achieve. </p>

python excel openpyxl