testMSOffice.py 5.8 KB

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