[python-win32] Problem about formatting a cell in Excel

Jeff Shannon jeff at ccvcorp.com
Tue Oct 5 20:03:18 CEST 2004


Lu Zheng wrote:

>Hi, all,
>
>I am trying to copy some strings from a txt file into Excel. And I found that,
>when I do this:
>       'xlWorksheet.Cells(a, b).Value = "%1"', 
>excel will format the string "%1" to "1%" when display and the actual value
>becomes 0.01
>
>How can I turn this autoformatting thing off so that "%1" will remain "%1"?
>  
>

Another approach is to explicitly set the NumberFormat on a cell / range 
of cells.

        r = sh.Range(sh.Cells(row1, col1), sh.Cells(row2, col2) )
        r.NumberFormat = '@'

The '@' format specifier indicates text.  I actually use a dict of my 
"standard" formats to make things a little easier for me:

        FormatDict = {
            'currency' : '$#,##0.00;[Red]-$#,##0.00',
            'date' : 'mm/dd/yy',
            'percent' : '0.0%',
            'text' : '@'
            }

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-win32 mailing list