[SciPy-dev] SciPy versus Matlab, Excel, and other tools

Kevin Altis altis at semi-retired.com
Thu Oct 25 12:08:00 EDT 2001


Don't shoot the messenger. :) My purpose in bringing up Excel was more about
the possibilities of using the graphing facilities via COM, which is a
Windows-only solution so it is a non-starter based on Eric's messages. I'm
not really a SciPy user, I was trying to better understand what facilities
SciPy needed that would benefit from collaboration with PythonCard, other
graphing tools or application frameworks. I'm actually starting work on a
more general framework than PythonCard, which will be specifically for
wxPython. The topic was brought up yesterday on the wxPython-users mailing
list.
http://aspn.activestate.com/ASPN/Mail/Message/wxPython-users/812294

Anyway, the win32com tools have an example of driving Excel with Python and
COM which I've included in part below. In the ActivePython distribution
(default install location), the files are:
  c:\python21\win32com\tests\testMSOffice.py
  c:\python21\win32com\tests\testMSOfficeEvents.py

You can also handle events. When I did some Python COM work earlier this
year I found the simplest solution was to use VB for the initial tests and
to browse the COM object properties and methods and then modify the syntax
as necessary for Python and then work in Python once the initial tests were
done. Just something to keep in mind.

The Excel COM model is documented on MSDN. Here's the Excel 2000 model
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modcore/htm
l/deovrMicrosoftExcel2000.asp

ka
---
def TextExcel(xl):
	xl.Visible = 0
	if xl.Visible: raise error, "Visible property is true."
	xl.Visible = 1
	if not xl.Visible: raise error, "Visible property not true."

	if int(xl.Version[0])>=8:
		xl.Workbooks.Add()
	else:
		xl.Workbooks().Add()


	xl.Range("A1:C1").Value = (1,2,3)
	xl.Range("A2:C2").Value = ('x','y','z')
	xl.Range("A3:C3").Value = ('3','2','1')

	for i in xrange(20):
		xl.Cells(i+1,i+1).Value = "Hi %d" % i

	if xl.Range("A1").Value <> "Hi 0":
		raise error, "Single cell range failed"

	if xl.Range("A1:B1").Value <> ((Unicode("Hi 0"),2),):
		raise error, "flat-horizontal cell range failed"

	if xl.Range("A1:A2").Value <> ((Unicode("Hi 0"),),(Unicode("x"),)):
		raise error, "flat-vertical cell range failed"

	if xl.Range("A1:C3").Value <> ((Unicode("Hi
0"),2,3),(Unicode("x"),Unicode("Hi 1"),Unicode("z")),(3,2,Unicode("Hi 2"))):
		raise error, "square cell range failed"

	xl.Range("A1:C3").Value =((3,2,1),("x","y","z"),(1,2,3))

	if xl.Range("A1:C3").Value  <>
((3,2,1),(Unicode("x"),Unicode("y"),Unicode("z")),(1,2,3)):
		raise error, "Range was not what I set it to!"

	# test dates out with Excel
	xl.Cells(5,1).Value = "Excel time"
	xl.Cells(5,2).Formula = "=Now()"

	import time
	xl.Cells(6,1).Value = "Python time"
	xl.Cells(6,2).Value = pythoncom.MakeTime(time.time())
	xl.Cells(6,2).NumberFormat = "d/mm/yy h:mm"
	xl.Columns("A:B").EntireColumn.AutoFit()

	xl.Workbooks(1).Close(0)
	xl.Quit()


def TestAll():
	try:
		TestWord()

		print "Starting Excel for Dynamic test..."
		xl = win32com.client.dynamic.Dispatch("Excel.Application")
		TextExcel(xl)

		try:
			print "Starting Excel 8 for generated excel8.py test..."
			mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 0,
1, 2, bForDemand=1)
			xl = win32com.client.Dispatch("Excel.Application")
			TextExcel(xl)
		except ImportError:
			print "Could not import the generated Excel 97 wrapper"

		try:
			import xl5en32
			mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 9,
1, 0)
			xl = win32com.client.Dispatch("Excel.Application.5")
			print "Starting Excel 95 for makepy test..."
			TextExcel(xl)
		except ImportError:
			print "Could not import the generated Excel 95 wrapper"

	except KeyboardInterrupt:
		print "*** Interrupted MSOffice test ***"
	except:
		traceback.print_exc()

if __name__=='__main__':
	TestAll()
	CheckClean()
	pythoncom.CoUninitialize()




More information about the SciPy-Dev mailing list