Column Width Autofit?

372 views
Skip to first unread message

Michael Hipp

unread,
Aug 26, 2011, 10:41:15 AM8/26/11
to python...@googlegroups.com
Is there any way to set a Column() width to "autofit"?

Thanks,
Michael

John Machin

unread,
Aug 26, 2011, 5:38:29 PM8/26/11
to python...@googlegroups.com


On Saturday, 27 August 2011 00:41:15 UTC+10, Michael Hipp wrote:
Is there any way to set a Column() width to "autofit"?


No. Autofit is something a user can do and then save the file. There is no provision for setting a flag so that Excel can  do it at file open time. xlwt doesn't have a rendering engine.

John Yeung

unread,
Aug 27, 2011, 12:48:57 AM8/27/11
to python...@googlegroups.com

It's not ludicrous to roll your own, though. (Well, not what I
consider ludicrous. Naturally others will disagree.) The simplest,
most accurate, and fastest-executing way would be to first pick a
monospaced font. The main trickiness then would be accounting for
formatting choices (numeric or date separators and the like).

Actually, in my opinion, that's the main trickiness regardless.
Compared to accounting for formatting, using a proportional font is
pretty easy, it's just more computationally intensive. If you have
access to the target machine (or an identically configured one), you
can even determine the relative widths of the characters empirically.
(I've done this for Arial 10 on the PC I use at work; this would be
approximately right for most folks using Arial. I can provide this
upon request.)

John Y.

XLS S

unread,
Aug 26, 2011, 7:30:52 PM8/26/11
to python...@googlegroups.com
Hey Michel,

Yes, is it possible using with VBA coding like (it only example)

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Columns("A:A").Select
    Range("A3").Activate
    Selection.Columns.AutoFit
End Sub

--
You received this message because you are subscribed to the Google Groups "python-excel" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/python-excel/-/W0pW6Pf7wF4J.
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.



--
.........................

IanKRolfe

unread,
Sep 19, 2011, 5:52:19 AM9/19/11
to python-excel
> Is there any way to set a Column() width to "autofit"?
>
Not as such but you can "guess" the column width and set it, as in the
code I wrote here:
http://textsnip.com/ae6ad7
(It doesn't handle all data types cleanly, but its good enough to show
you what to do!)

This gets the column width mostly correct by sizing the field to the
length of the longest one/

mic...@redmule.com

unread,
Sep 21, 2011, 11:36:42 AM9/21/11
to python...@googlegroups.com
On 9/19/2011 4:52 AM, IanKRolfe wrote:
>> Is there any way to set a Column() width to "autofit"?
>>
> Not as such but you can "guess" the column width and set it, as in the
> code I wrote here:
> http://textsnip.com/ae6ad7
> (It doesn't handle all data types cleanly, but its good enough to show
> you what to do!)
Thanks for this.

Michael

Reply all
Reply to author
Forward
0 new messages