Re: Cast general numeric cell into text



Hi All,

I tried a few options as suggested, but they didn't work out completely for me. I did find a solution after all, and I'm surprised that it wasn't too obscure (although not apparent from my forum searching). However, this solution requires Windows/Excel/COM to work. Here is my example problem and solution:

Make an Excel spreadsheet at C:\Book1.xls with A1 = "23.2524542456546" which displays "23.25245"

#Python:
import win32com.client # http://sourceforge.net/projects/pywin32/
xl = win32com.client.Dispatch("Excel.Application")
wb = xl.Workbooks.Open(r'C:\book1.xls')
ws = wb.Worksheets(1)
print repr(ws2.Range('A1').Value)  # 23.252454245654601
print ws2.Range('A1').Text         # 23.25245


So in summary, use the .Text attribute for the Cell COM Range object, and it will cast the viewable text for you. The .Value is a variant type, and can be one of several classes, including float for this example (BTW, I'm storing both in my DB).

(Unfortunately, my Win32/Excel/Python skills are better than my Gnumeric/Python skills; otherwise I'd shine some light to the solution for Gnumeric users.)

+mt



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]