home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32com_test_testMSOffice.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  5.2 KB  |  175 lines

  1. # Test MSOffice
  2. #
  3. # Main purpose of test is to ensure that Dynamic COM objects
  4. # work as expected.
  5.  
  6. # Assumes Word and Excel installed on your machine.
  7.  
  8. import win32com, sys, string, win32api, traceback
  9. import win32com.client.dynamic
  10. from win32com.test.util import CheckClean
  11. import pythoncom
  12. from win32com.client import gencache
  13. from pywintypes import Unicode
  14.  
  15. error = "MSOffice test error"
  16.  
  17. # Test a few of the MSOffice components.
  18. def TestWord():
  19.     # Try and load the object exposed by Word 8
  20.     # Office 97 - _totally_ different object model!
  21.     try:
  22.         # NOTE - using "client.Dispatch" would return an msword8.py instance!
  23.         print "Starting Word 8 for dynamic test"
  24.         word = win32com.client.dynamic.Dispatch("Word.Application")
  25.         TestWord8(word)
  26.  
  27.         word = None        
  28.         # Now we will test Dispatch without the new "lazy" capabilities
  29.         print "Starting Word 8 for non-lazy dynamic test"
  30.         dispatch = win32com.client.dynamic._GetGoodDispatch("Word.Application")
  31.         typeinfo = dispatch.GetTypeInfo()
  32.         attr = typeinfo.GetTypeAttr()
  33.         olerepr = win32com.client.build.DispatchItem(typeinfo, attr, None, 0)
  34.         word = win32com.client.dynamic.CDispatch(dispatch, olerepr)
  35.         dispatch = typeinfo = attr = olerepr = None
  36.         TestWord8(word)
  37.  
  38.     except pythoncom.com_error:
  39.         print "Starting Word 7 for dynamic test"
  40.         word = win32com.client.Dispatch("Word.Basic")
  41.         TestWord7(word)
  42.     
  43.     try:
  44.         print "Starting MSWord for generated test"
  45.         # Typelib, lcid, major and minor for the typelib
  46.         if gencache.EnsureModule("{00020905-0000-0000-C000-000000000046}", 1033, 8, 1, bForDemand=1) is None :
  47.             raise ImportError, "Can not load the Word8 typelibrary."
  48.         word = win32com.client.Dispatch("Word.Application.8")
  49.         TestWord8(word)
  50.     except ImportError, details:
  51.         print "Can not test MSWord8 -", details
  52.  
  53. def TestWord7(word):
  54.     word.FileNew()
  55.     # If not shown, show the app.
  56.     if not word.AppShow(): word._proc_("AppShow")
  57.         
  58.     for i in xrange(12):
  59.         word.FormatFont(Color=i+1, Points=i+12)
  60.         word.Insert("Hello from Python %d\n" % i)
  61.     
  62.     word.FileClose(2)
  63.  
  64. def TestWord8(word):
  65.     word.Visible = 1
  66.     doc = word.Documents.Add()
  67.     wrange = doc.Range()
  68.     for i in range(10):
  69.         wrange.InsertAfter("Hello from Python %d\n" % i)
  70.     paras = doc.Paragraphs
  71.     for i in range(len(paras)):
  72.         paras[i]().Font.ColorIndex = i+1
  73.         paras[i]().Font.Size = 12 + (4 * i)
  74.     # XXX - note that
  75.     # for para in paras:
  76.     #     para().Font...
  77.     # doesnt seem to work - no error, just doesnt work
  78.     # Should check if it works for VB!
  79.     doc.Close(SaveChanges = 0)
  80.     word.Quit()
  81.     win32api.Sleep(1000) # Wait for word to close, else we
  82.     # may get OA error.
  83.  
  84. def TestWord8OldStyle():
  85.     try:
  86.         import win32com.test.Generated4Test.msword8
  87.     except ImportError:
  88.         print "Can not do old style test"
  89.  
  90.     
  91. def TextExcel(xl):
  92.     xl.Visible = 0
  93.     if xl.Visible: raise error, "Visible property is true."
  94.     xl.Visible = 1
  95.     if not xl.Visible: raise error, "Visible property not true."
  96.  
  97.     if int(xl.Version[0])>=8:
  98.         xl.Workbooks.Add()
  99.     else:
  100.         xl.Workbooks().Add()
  101.         
  102.  
  103.     xl.Range("A1:C1").Value = (1,2,3)
  104.     xl.Range("A2:C2").Value = ('x','y','z')
  105.     xl.Range("A3:C3").Value = ('3','2','1')
  106.  
  107.     for i in xrange(20):
  108.         xl.Cells(i+1,i+1).Value = "Hi %d" % i
  109.  
  110.     if xl.Range("A1").Value <> "Hi 0":
  111.         raise error, "Single cell range failed"
  112.  
  113.     if xl.Range("A1:B1").Value <> ((Unicode("Hi 0"),2),):
  114.         raise error, "flat-horizontal cell range failed"
  115.  
  116.     if xl.Range("A1:A2").Value <> ((Unicode("Hi 0"),),(Unicode("x"),)):
  117.         raise error, "flat-vertical cell range failed"
  118.  
  119.     if xl.Range("A1:C3").Value <> ((Unicode("Hi 0"),2,3),(Unicode("x"),Unicode("Hi 1"),Unicode("z")),(3,2,Unicode("Hi 2"))):
  120.         raise error, "square cell range failed"
  121.  
  122.     xl.Range("A1:C3").Value =((3,2,1),("x","y","z"),(1,2,3))
  123.  
  124.     if xl.Range("A1:C3").Value  <> ((3,2,1),(Unicode("x"),Unicode("y"),Unicode("z")),(1,2,3)):
  125.         raise error, "Range was not what I set it to!"
  126.  
  127.     # test dates out with Excel
  128.     xl.Cells(5,1).Value = "Excel time"
  129.     xl.Cells(5,2).Formula = "=Now()"
  130.  
  131.     import time
  132.     xl.Cells(6,1).Value = "Python time"
  133.     xl.Cells(6,2).Value = pythoncom.MakeTime(time.time())
  134.     xl.Cells(6,2).NumberFormat = "d/mm/yy h:mm"
  135.     xl.Columns("A:B").EntireColumn.AutoFit()
  136.  
  137.     xl.Workbooks(1).Close(0)
  138.     xl.Quit()
  139.  
  140. def TestAll():
  141.     try:
  142.         TestWord()
  143.  
  144.         print "Starting Excel for Dynamic test..."
  145.         xl = win32com.client.dynamic.Dispatch("Excel.Application")
  146.         TextExcel(xl)
  147.  
  148.         try:
  149.             print "Starting Excel 8 for generated excel8.py test..."
  150.             mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 0, 1, 2, bForDemand=1)
  151.             xl = win32com.client.Dispatch("Excel.Application")
  152.             TextExcel(xl)
  153.         except ImportError:
  154.             print "Could not import the generated Excel 97 wrapper"
  155.  
  156.         try:
  157.             import xl5en32
  158.             mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 9, 1, 0)
  159.             xl = win32com.client.Dispatch("Excel.Application.5")
  160.             print "Starting Excel 95 for makepy test..."
  161.             TextExcel(xl)
  162.         except ImportError:
  163.             print "Could not import the generated Excel 95 wrapper"
  164.  
  165.     except KeyboardInterrupt:
  166.         print "*** Interrupted MSOffice test ***"
  167.     except:
  168.         traceback.print_exc()
  169.  
  170. if __name__=='__main__':
  171.     TestAll()
  172.     CheckClean()
  173.     pythoncom.CoUninitialize()
  174.  
  175.