Autofit Selection

58 views
Skip to first unread message

Warwick Prince

unread,
Jan 30, 2012, 6:00:09 AM1/30/12
to python-excel
Hi All

New to python-excel but very impressed. One thing I needed was to be
able to "AutoFit" a column width to the data it contained. I looked
around and found that it's not supported due to it being a "render-
time" function of Excel - so - I wrote one :-)

It's a working beta at the moment, and I wondered if anyone else was
interested? If so, I'll complete testing as much as I can shortly,
and put it up. Some details on how to post my changes would be
appreciated as I'm not a regular contributor to "groups".

In case you are wondering, it deals with Normal/Bolded values,
formatted numbers like $1,000.00 or 1000 and dates/times 17-MAR or
17/03/2011 10:34 pm or 10:34:00 etc. etc. Font size is also
considered in the process of course. The "base font" is Arial,
however I'm *pretty sure* that the factoring process I've built in
should cater for other fonts as well. In the case of a *very
different* font from Arial, like Courier New for example, one can
simply overload the Font class and build in the character width
mappings to suit. It's pretty good at the moment, and will improve -
however, it's 100% better than attempting to guess widths.

One just needs to xlwt.Autofit.Autofit(sheet, fromColx, toColx) and
your done. I'm going to change that shortly however to allow a row
range as well. This can be done at any time (assuming you have not
flushed the rows) but the intention is that you would call it at the
end before you save.

Had a lot of fun today writing it - and learned a lot about how xlwt
works in the process - nice :-)

Cheers
Warwick

Craig Barnes

unread,
Jan 30, 2012, 6:42:20 AM1/30/12
to python...@googlegroups.com
Hi Warwick,

I could really use this, ATM I am padding my column headings with white-space and using that length to set the width.

Do you have your code on a public repository  (e.g. github[0] or bitbucket[1])
that you could make available?
Alternatively
you could paste your code in pastebin[2].


[0] https://github.com/
[1] https://bitbucket.org/
[2] http://pastebin.com/

Regards

Craig

Warwick

--
You received this message because you are subscribed to the Google Groups "python-excel" group.
To post to this group, send an email to python...@googlegroups.com.
To unsubscribe from this group, send email to python-excel...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/python-excel?hl=en-GB.




--
Craig

()  ascii ribbon campaign - against html mail
/\

Warwick Prince

unread,
Jan 30, 2012, 9:09:55 PM1/30/12
to python-excel
Hi Craig

Happy to put it up somewhere. I'm just finishing off a few aspects,
and re-thinking some of the architecture.. I originally embedded some
of the logic into the parts that made sense (like adding font specific
things into the Formatting.Font class. This makes it harder to simply
supply a "plugin" for Autofit, so I'm moving it into it's own area and
calling out to xlwt where necessary. Will not take too long.

Will post details here when available.

Cheers
Warwick

Adrian Klaver

unread,
Jan 31, 2012, 10:18:25 AM1/31/12
to python...@googlegroups.com, Warwick Prince

+1

>
> Cheers
> Warwick

--
Adrian Klaver
adrian...@gmail.com

Chris Withers

unread,
Feb 1, 2012, 2:49:45 AM2/1/12
to python...@googlegroups.com, Warwick Prince
Hi Warwick,

On 30/01/2012 11:00, Warwick Prince wrote:
> It's a working beta at the moment, and I wondered if anyone else was
> interested? If so, I'll complete testing as much as I can shortly,
> and put it up. Some details on how to post my changes would be
> appreciated as I'm not a regular contributor to "groups".

I'd be very interested in working this into xlwt, if that's okay with you?

If so, please can you let me know how you've been developing this?
Is it in source control anywhere?

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

Reply all
Reply to author
Forward
0 new messages