[python-win32] Fwd: Autosizing column widths in Excel using win32com.client ?

Tim Golden mail at timgolden.me.uk
Sun May 17 12:00:08 CEST 2009


James Matthews wrote:
> ---------- Forwarded message ----------
> From: <nonsense at mynonsense.net>
> Date: Fri, May 15, 2009 at 7:45 PM
> Subject: Autosizing column widths in Excel using win32com.client ?
> To: python-list at python.org
> 
> 
> Is there a way to autosize the widths of the excel columns as when you
> double click them manually?

Usual answer to this kind of question: record a macro
in Excel to do what you want, and then use COM to
automate that. On my Excel 2007, this is the VBA result
of recording:

<vba>
Sub Macro2()
'
' Macro2 Macro
'
'
    Columns("A:A").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Columns("A:D").EntireColumn.AutoFit
End Sub
</vba>


You then just fire up win32com.client or comtypes,
according to taste and go from there. If you need
help with the COM side of things, post back here.

TJG


More information about the python-win32 mailing list