[python-win32] Excel worksheet range, Text property

Andrea Gavana andrea.gavana at gmail.com
Tue Aug 9 11:42:36 CEST 2011


Hi All,

    I am trying to speed up a bit a new widget I created for wxPython, which
uses heavily the COM interface between Python and Excel (it only reads Excel
data, no writing).

One of the main issues I have found is related to the extraction of the
"Text" property from a range of cells. Let's suppose I have a very simple
Excel file (I attach a dummy sample of it). This file contains cells with
some value in them, other cells are empty. When I run the following script:


import os
from win32com.client import Dispatch

xlApp = Dispatch('Excel.Application')

filename, sheetname = os.path.join(os.getcwd(), "Book1.xls"), "Book1"

xlBook = xlApp.Workbooks.Open(filename)
xlSheet = xlBook.Worksheets(sheetname)

print "Values:"
print xlSheet.Range("A1:K11").Value

print "\n\n"

print "Texts:"
print xlSheet.Range("A1:K11").Text


I get a 2D nested tuple of values for the "Value" property, but I only get a
single "None" for the "Text" property (i.e., no tuple, just "None"). The
solution I came up with is to iterate on every cell in the spreadsheet and
get its "Text" property if it is not "None", but this is expensive compared
to the "vectorized" approach you can get with Range().

Does anyone have a suggestion on how to improve my current approach (or how
to fix my flawed code above)?

Thank you in advance for your help.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20110809/0edd19b3/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Book1.xls
Type: application/vnd.ms-excel
Size: 14336 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-win32/attachments/20110809/0edd19b3/attachment-0001.xls>


More information about the python-win32 mailing list