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

Jeff Shannon jeff at ccvcorp.com
Wed Oct 6 19:56:28 CEST 2004


Lu Zheng wrote:

>Great, this works. I wonder how I can use your FormatDict. How can I put it in?
>
>
>  
>

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

    def formatrange(format, row1, col1, row2, col2):
        range = sheet.Range(sheet.Cells(row1, col1), sheet.Cells(row2, 
col2) )
        range.NumberFormat = FormatDict['text']

    # format first four columns / 20 rows as text
    formatrange('text', 1, 1, 20, 4)
    # format next column as currency
    formatrange('currency', 1, 5, 20, 5)

Here, FormatDict is a global variable.  When I need to format a range of 
cells, I simply get the a reference to the range using normal methods, 
and set the NumberFormat property of that range to one of the values 
held in the FormatDict.  The dict just lets me use a human-readable 
description of the format instead of a potentially-obscure series of 
punctuation characters.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-win32 mailing list