Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testMSOffice.py 5.9KB

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