Excel Ranges from Python COM server

Don don_b at my-deja.com
Tue Feb 1 10:13:25 EST 2000


I am having trouble setting getting data from Python to set a range in
Excel VBA.  From Python I can manipulate Excel ranges as tuples of
tuples, just fine.  But I am doing something wrong calling Python from
VB.  The following example demonstrates my problem.

An Excel Range gives me a Python tuple of tuples in Python, but a Python
typle of tuples is not an Excel Range in Excel.  What should my
function, getRange return?

>>> import win32com.client
>>> xl = win32com.client.Dispatch("Excel.Application.9")
>>> xl.Workbooks.Add()
<COMObject <unknown>>
>>> xl.Visible = 1
>>> xl.Range("A1:B2").Value
((None, None), (None, None))
>>> data = (('one','two'),('three','four'))
>>> xl.Range("A1:B2").Value = data
>>> xl.Range("A1:B2").Value
((L'one', L'two'), (L'three', L'four'))

However, if I make a simple Python COM server...

class RangeTest:
    _public_methods_ = ['getRange']
    _reg_progid_ = "test.rangeTest"
    # print pythoncom.CreateGuid()
    _reg_clsid_ = "{F3514F94-D8B1-11D3-8A13-00A0244A1060}"

    def getRange(self):
        return (("one","two"),("three","four"))

if __name__ == '__main__':
    import win32com.server.register
    win32com.server.register.UseCommandLine(RangeTest)

>From an Excel spreadsheet I call...

Sub rangeTest()
    Dim py As Object
    Set py = CreateObject("test.rangeTest")
    Range("A1:B2").Value = py.getRange()
End Sub

The results in my spreadsheet are...
	A	B
1	one	two
2	one	two

BUT, what I wanted to have in the spreadsheet was...
	A	B
1	one	two
2	three	four


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list