Formmating excel cells with PyExcelerator or COM

Tim Golden tim.golden at viacom-outdoor.co.uk
Mon May 15 08:47:00 EDT 2006


[Mauricio Tellez]

| Hi, I just want that a number like 1234.123 appear in excel 
| as 1,234.12
| In excel I just select some cells, then right click on them 
| and select "Cell Formatting" then select Number, and check 
| "Use thounsands separator" and 2 decimal places. I can't find 
| how to do this with PyExcelerator neither with COM. Any clue? 

Nearly always, a good starting point for doing this
kind of thing with COM (obviously doesn't apply for
PyExcelerator) is to record a Macro in Excel itself
which does what you want, and then to translate the
code that macro uses into Python -- usually trivial.

Here, I stuck a number into Excel, applied formatting
as you described, and the result was:

Selection.NumberFormat = "#,##0.00"

Obviously you have to do whatever you need around
that to apply the formatting to the range of values
you're interested in, but a (negligible) working
example might be:

<code>
import win32com.client

xl = win32com.client.gencache.EnsureDispatch ("Excel.Application")
xl.Visible = 1
wb = xl.Workbooks.Add ()
ws = wb.ActiveSheet

range = ws.Range (ws.Cells (1, 1), ws.Cells (1, 3))
range.Value = [1234, 2345, 3456]
range.NumberFormat = "#,##0.00"

</code>

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list