Python Forum

Full Version: Setting column auto_size property
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I am creating an Excel workbook using openpyxl. I build the worksheets - which is not a rocket science  Cool, but I cannot find a way to set bestFit, auto_size, whatever  Huh attribute of columns.

I was pointed to dimesions module in documentation - but it allows you to check the attribute, not to set it.

I tried browsing styles module - no luck there too. 

Could anyone please help me out? 
(To stave questions "what I have tried" - I cannot show anything because I did not find anything worth trying)
I have no experience with openpyxl, but glancing at the source here:  
http://openpyxl.readthedocs.io/en/defaul...sions.html

I can see that there are two classes (which inherit from Dimension) which have the arguments you are looking for as attributes.
They all have default vaules, but you can also pass them on creation of an instance setting them to whatever you want.
(Apr-15-2017, 11:15 AM)Mekire Wrote: [ -> ]I have no experience with openpyxl, but glancing at the source here:  
http://openpyxl.readthedocs.io/en/defaul...sions.html

I can see that there are two classes (which inherit from Dimension) which have the arguments you are looking for as attributes.
They all have default vaules, but you can also pass them on creation of an instance setting them to whatever you want.

Exactly my problem - it is unclear how they may be connected to sheet, and the documentation specifically states that they are used for querying - not setting (This was the link I failed to include since it was my first post, in this incarnation  Cool)
I tried that

sheet = wb.create_sheet(' By '.join((sheet_name, test_type)))
sheet.append(columns)
for dim in sheet.column_dimensions.values():
  dim.bestFit = True
It did not work - though by documentation I did not really expect it to