PyXR

c:\python24\lib\site-packages\win32 \ com \ test \ testMSOffice.py



0001 # Test MSOffice
0002 #
0003 # Main purpose of test is to ensure that Dynamic COM objects
0004 # work as expected.
0005 
0006 # Assumes Word and Excel installed on your machine.
0007 
0008 import win32com, sys, string, win32api, traceback
0009 import win32com.client.dynamic
0010 from win32com.test.util import CheckClean
0011 import pythoncom
0012 from win32com.client import gencache
0013 from pywintypes import Unicode
0014 
0015 error = "MSOffice test error"
0016 
0017 # Test a few of the MSOffice components.
0018 def TestWord():
0019     # Try and load the object exposed by Word 8
0020     # Office 97 - _totally_ different object model!
0021     try:
0022         # NOTE - using "client.Dispatch" would return an msword8.py instance!
0023         print "Starting Word 8 for dynamic test"
0024         word = win32com.client.dynamic.Dispatch("Word.Application")
0025         TestWord8(word)
0026 
0027         word = None
0028         # Now we will test Dispatch without the new "lazy" capabilities
0029         print "Starting Word 8 for non-lazy dynamic test"
0030         dispatch = win32com.client.dynamic._GetGoodDispatch("Word.Application")
0031         typeinfo = dispatch.GetTypeInfo()
0032         attr = typeinfo.GetTypeAttr()
0033         olerepr = win32com.client.build.DispatchItem(typeinfo, attr, None, 0)
0034         word = win32com.client.dynamic.CDispatch(dispatch, olerepr)
0035         dispatch = typeinfo = attr = olerepr = None
0036         TestWord8(word)
0037 
0038     except pythoncom.com_error:
0039         print "Starting Word 7 for dynamic test"
0040         word = win32com.client.Dispatch("Word.Basic")
0041         TestWord7(word)
0042 
0043     print "Starting MSWord for generated test"
0044     from win32com.client import gencache
0045     word = gencache.EnsureDispatch("Word.Application.8")
0046     TestWord8(word)
0047 
0048 def TestWord7(word):
0049     word.FileNew()
0050     # If not shown, show the app.
0051     if not word.AppShow(): word._proc_("AppShow")
0052 
0053     for i in xrange(12):
0054         word.FormatFont(Color=i+1, Points=i+12)
0055         word.Insert("Hello from Python %d\n" % i)
0056 
0057     word.FileClose(2)
0058 
0059 def TestWord8(word):
0060     word.Visible = 1
0061     doc = word.Documents.Add()
0062     wrange = doc.Range()
0063     for i in range(10):
0064         wrange.InsertAfter("Hello from Python %d\n" % i)
0065     paras = doc.Paragraphs
0066     for i in range(len(paras)):
0067         p = paras[i]()
0068         p.Font.ColorIndex = i+1
0069         p.Font.Size = 12 + (4 * i)
0070     # XXX - note that
0071     # for para in paras:
0072     #       para().Font...
0073     # doesnt seem to work - no error, just doesnt work
0074     # Should check if it works for VB!
0075     doc.Close(SaveChanges = 0)
0076     word.Quit()
0077     win32api.Sleep(1000) # Wait for word to close, else we
0078     # may get OA error.
0079 
0080 def TestWord8OldStyle():
0081     try:
0082         import win32com.test.Generated4Test.msword8
0083     except ImportError:
0084         print "Can not do old style test"
0085 
0086 
0087 def TextExcel(xl):
0088     xl.Visible = 0
0089     if xl.Visible: raise error, "Visible property is true."
0090     xl.Visible = 1
0091     if not xl.Visible: raise error, "Visible property not true."
0092 
0093     if int(xl.Version[0])>=8:
0094         xl.Workbooks.Add()
0095     else:
0096         xl.Workbooks().Add()
0097 
0098 
0099     xl.Range("A1:C1").Value = (1,2,3)
0100     xl.Range("A2:C2").Value = ('x','y','z')
0101     xl.Range("A3:C3").Value = ('3','2','1')
0102 
0103     for i in xrange(20):
0104         xl.Cells(i+1,i+1).Value = "Hi %d" % i
0105 
0106     if xl.Range("A1").Value <> "Hi 0":
0107         raise error, "Single cell range failed"
0108 
0109     if xl.Range("A1:B1").Value <> ((Unicode("Hi 0"),2),):
0110         raise error, "flat-horizontal cell range failed"
0111 
0112     if xl.Range("A1:A2").Value <> ((Unicode("Hi 0"),),(Unicode("x"),)):
0113         raise error, "flat-vertical cell range failed"
0114 
0115     if xl.Range("A1:C3").Value <> ((Unicode("Hi 0"),2,3),(Unicode("x"),Unicode("Hi 1"),Unicode("z")),(3,2,Unicode("Hi 2"))):
0116         raise error, "square cell range failed"
0117 
0118     xl.Range("A1:C3").Value =((3,2,1),("x","y","z"),(1,2,3))
0119 
0120     if xl.Range("A1:C3").Value  <> ((3,2,1),(Unicode("x"),Unicode("y"),Unicode("z")),(1,2,3)):
0121         raise error, "Range was not what I set it to!"
0122 
0123     # test dates out with Excel
0124     xl.Cells(5,1).Value = "Excel time"
0125     xl.Cells(5,2).Formula = "=Now()"
0126 
0127     import time
0128     xl.Cells(6,1).Value = "Python time"
0129     xl.Cells(6,2).Value = pythoncom.MakeTime(time.time())
0130     xl.Cells(6,2).NumberFormat = "d/mm/yy h:mm"
0131     xl.Columns("A:B").EntireColumn.AutoFit()
0132 
0133     xl.Workbooks(1).Close(0)
0134     xl.Quit()
0135 
0136 def TestAll():
0137     try:
0138         TestWord()
0139 
0140         print "Starting Excel for Dynamic test..."
0141         xl = win32com.client.dynamic.Dispatch("Excel.Application")
0142         TextExcel(xl)
0143 
0144         try:
0145             print "Starting Excel 8 for generated excel8.py test..."
0146             mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 0, 1, 2, bForDemand=1)
0147             xl = win32com.client.Dispatch("Excel.Application")
0148             TextExcel(xl)
0149         except ImportError:
0150             print "Could not import the generated Excel 97 wrapper"
0151 
0152         try:
0153             import xl5en32
0154             mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 9, 1, 0)
0155             xl = win32com.client.Dispatch("Excel.Application.5")
0156             print "Starting Excel 95 for makepy test..."
0157             TextExcel(xl)
0158         except ImportError:
0159             print "Could not import the generated Excel 95 wrapper"
0160 
0161     except KeyboardInterrupt:
0162         print "*** Interrupted MSOffice test ***"
0163     except:
0164         traceback.print_exc()
0165 
0166 if __name__=='__main__':
0167     TestAll()
0168     CheckClean()
0169     pythoncom.CoUninitialize()
0170 

Generated by PyXR 0.9.4
SourceForge.net Logo