Python Forum
Setting column auto_size property
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting column auto_size property
#1
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)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#2
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.
Reply
#3
(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)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,802 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,691 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,345 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Can property getters and setters have additional arguments? pjfarley3 2 3,053 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  Property price calculation oli_action 4 3,178 Jul-15-2020, 04:27 PM
Last Post: sridhar
  Use of @property decorator ruy 16 6,570 Jun-09-2020, 05:29 PM
Last Post: buran
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,059 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 2,365 Nov-25-2019, 04:32 PM
Last Post: KaliLinux
  print all method and property of list object engmoh 4 2,867 Oct-26-2019, 05:33 PM
Last Post: engmoh
  group by property bhojendra 25 8,369 Jul-01-2019, 08:45 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020