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.

excelAddin.py 6.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # A demo plugin for Microsoft Excel
  2. #
  3. # This addin simply adds a new button to the main Excel toolbar,
  4. # and displays a message box when clicked. Thus, it demonstrates
  5. # how to plug in to Excel itself, and hook Excel events.
  6. #
  7. #
  8. # To register the addin, simply execute:
  9. # excelAddin.py
  10. # This will install the COM server, and write the necessary
  11. # AddIn key to Excel
  12. #
  13. # To unregister completely:
  14. # excelAddin.py --unregister
  15. #
  16. # To debug, execute:
  17. # excelAddin.py --debug
  18. #
  19. # Then open Pythonwin, and select "Tools->Trace Collector Debugging Tool"
  20. # Restart excel, and you should see some output generated.
  21. #
  22. # NOTE: If the AddIn fails with an error, Excel will re-register
  23. # the addin to not automatically load next time Excel starts. To
  24. # correct this, simply re-register the addin (see above)
  25. #
  26. # Author <ekoome@yahoo.com> Eric Koome
  27. # Copyright (c) 2003 Wavecom Inc. All rights reserved
  28. #
  29. # Redistribution and use in source and binary forms, with or without
  30. # modification, are permitted provided that the following conditions
  31. # are met:
  32. #
  33. # 1. Redistributions of source code must retain the above copyright
  34. # notice, this list of conditions and the following disclaimer.
  35. #
  36. # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. # DISCLAIMED. IN NO EVENT SHALL ERIC KOOME OR
  40. # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. # SUCH DAMAGE.
  48. import sys
  49. import pythoncom
  50. from win32com import universal
  51. from win32com.client import Dispatch, DispatchWithEvents, constants, gencache
  52. from win32com.server.exception import COMException
  53. # Support for COM objects we use.
  54. gencache.EnsureModule(
  55. "{00020813-0000-0000-C000-000000000046}", 0, 1, 3, bForDemand=True
  56. ) # Excel 9
  57. gencache.EnsureModule(
  58. "{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}", 0, 2, 1, bForDemand=True
  59. ) # Office 9
  60. # The TLB defining the interfaces we implement
  61. universal.RegisterInterfaces(
  62. "{AC0714F2-3D04-11D1-AE7D-00A0C90F26F4}", 0, 1, 0, ["_IDTExtensibility2"]
  63. )
  64. class ButtonEvent:
  65. def OnClick(self, button, cancel):
  66. import win32con # Possible, but not necessary, to use a Pythonwin GUI
  67. import win32ui
  68. win32ui.MessageBox("Hello from Python", "Python Test", win32con.MB_OKCANCEL)
  69. return cancel
  70. class ExcelAddin:
  71. _com_interfaces_ = ["_IDTExtensibility2"]
  72. _public_methods_ = []
  73. _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER
  74. _reg_clsid_ = "{C5482ECA-F559-45A0-B078-B2036E6F011A}"
  75. _reg_progid_ = "Python.Test.ExcelAddin"
  76. _reg_policy_spec_ = "win32com.server.policy.EventHandlerPolicy"
  77. def __init__(self):
  78. self.appHostApp = None
  79. def OnConnection(self, application, connectMode, addin, custom):
  80. print("OnConnection", application, connectMode, addin, custom)
  81. try:
  82. self.appHostApp = application
  83. cbcMyBar = self.appHostApp.CommandBars.Add(
  84. Name="PythonBar",
  85. Position=constants.msoBarTop,
  86. MenuBar=constants.msoBarTypeNormal,
  87. Temporary=True,
  88. )
  89. btnMyButton = cbcMyBar.Controls.Add(
  90. Type=constants.msoControlButton, Parameter="Greetings"
  91. )
  92. btnMyButton = self.toolbarButton = DispatchWithEvents(
  93. btnMyButton, ButtonEvent
  94. )
  95. btnMyButton.Style = constants.msoButtonCaption
  96. btnMyButton.BeginGroup = True
  97. btnMyButton.Caption = "&Python"
  98. btnMyButton.TooltipText = "Python rules the World"
  99. btnMyButton.Width = "34"
  100. cbcMyBar.Visible = True
  101. except pythoncom.com_error as xxx_todo_changeme:
  102. (hr, msg, exc, arg) = xxx_todo_changeme.args
  103. print("The Excel call failed with code %d: %s" % (hr, msg))
  104. if exc is None:
  105. print("There is no extended error information")
  106. else:
  107. wcode, source, text, helpFile, helpId, scode = exc
  108. print("The source of the error is", source)
  109. print("The error message is", text)
  110. print("More info can be found in %s (id=%d)" % (helpFile, helpId))
  111. def OnDisconnection(self, mode, custom):
  112. print("OnDisconnection")
  113. self.appHostApp.CommandBars("PythonBar").Delete
  114. self.appHostApp = None
  115. def OnAddInsUpdate(self, custom):
  116. print("OnAddInsUpdate", custom)
  117. def OnStartupComplete(self, custom):
  118. print("OnStartupComplete", custom)
  119. def OnBeginShutdown(self, custom):
  120. print("OnBeginShutdown", custom)
  121. def RegisterAddin(klass):
  122. import winreg
  123. key = winreg.CreateKey(
  124. winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Excel\\Addins"
  125. )
  126. subkey = winreg.CreateKey(key, klass._reg_progid_)
  127. winreg.SetValueEx(subkey, "CommandLineSafe", 0, winreg.REG_DWORD, 0)
  128. winreg.SetValueEx(subkey, "LoadBehavior", 0, winreg.REG_DWORD, 3)
  129. winreg.SetValueEx(subkey, "Description", 0, winreg.REG_SZ, "Excel Addin")
  130. winreg.SetValueEx(subkey, "FriendlyName", 0, winreg.REG_SZ, "A Simple Excel Addin")
  131. def UnregisterAddin(klass):
  132. import winreg
  133. try:
  134. winreg.DeleteKey(
  135. winreg.HKEY_CURRENT_USER,
  136. "Software\\Microsoft\\Office\\Excel\\Addins\\" + klass._reg_progid_,
  137. )
  138. except WindowsError:
  139. pass
  140. if __name__ == "__main__":
  141. import win32com.server.register
  142. win32com.server.register.UseCommandLine(ExcelAddin)
  143. if "--unregister" in sys.argv:
  144. UnregisterAddin(ExcelAddin)
  145. else:
  146. RegisterAddin(ExcelAddin)