msoffice.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # This demo uses some of the Microsoft Office components.
  2. #
  3. # It was taken from an MSDN article showing how to embed excel.
  4. # It is not comlpete yet, but it _does_ show an Excel spreadsheet in a frame!
  5. #
  6. import win32ui, win32uiole, win32con, regutil
  7. from pywin.mfc import window, activex, object, docview
  8. from win32com.client import gencache
  9. #WordModule = gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 1033, 8, 0)
  10. #if WordModule is None:
  11. # raise ImportError, "Microsoft Word version 8 does not appear to be installed."
  12. class OleClientItem(object.CmdTarget):
  13. def __init__(self, doc):
  14. object.CmdTarget.__init__(self, win32uiole.CreateOleClientItem(doc))
  15. def OnGetItemPosition(self):
  16. # For now return a hard-coded rect.
  17. return (10, 10, 210, 210)
  18. def OnActivate(self):
  19. # Allow only one inplace activate item per frame
  20. view = self.GetActiveView()
  21. item = self.GetDocument().GetInPlaceActiveItem(view)
  22. if item is not None and item._obj_ != self._obj_:
  23. item.Close()
  24. self._obj_.OnActivate()
  25. def OnChange(self, oleNotification, dwParam):
  26. self._obj_.OnChange(oleNotification, dwParam)
  27. self.GetDocument().UpdateAllViews(None)
  28. def OnChangeItemPosition(self, rect):
  29. # During in-place activation CEmbed_ExcelCntrItem::OnChangeItemPosition
  30. # is called by the server to change the position of the in-place
  31. # window. Usually, this is a result of the data in the server
  32. # document changing such that the extent has changed or as a result
  33. # of in-place resizing.
  34. #
  35. # The default here is to call the base class, which will call
  36. # COleClientItem::SetItemRects to move the item
  37. # to the new position.
  38. if not self._obj_.OnChangeItemPosition(self, rect):
  39. return 0
  40. # TODO: update any cache you may have of the item's rectangle/extent
  41. return 1
  42. class OleDocument(object.CmdTarget):
  43. def __init__(self, template):
  44. object.CmdTarget.__init__(self, win32uiole.CreateOleDocument(template))
  45. self.EnableCompoundFile()
  46. class ExcelView(docview.ScrollView):
  47. def OnInitialUpdate(self):
  48. self.HookMessage(self.OnSetFocus, win32con.WM_SETFOCUS)
  49. self.HookMessage (self.OnSize, win32con.WM_SIZE)
  50. self.SetScrollSizes(win32con.MM_TEXT, (100, 100))
  51. rc = self._obj_.OnInitialUpdate()
  52. self.EmbedExcel()
  53. return rc
  54. def EmbedExcel(self):
  55. doc = self.GetDocument()
  56. self.clientItem = OleClientItem(doc)
  57. self.clientItem.CreateNewItem("Excel.Sheet")
  58. self.clientItem.DoVerb(-1, self)
  59. doc.UpdateAllViews(None)
  60. def OnDraw(self, dc):
  61. doc = self.GetDocument()
  62. pos = doc.GetStartPosition()
  63. clientItem, pos = doc.GetNextItem(pos)
  64. clientItem.Draw(dc, (10, 10, 210, 210) )
  65. # Special handling of OnSetFocus and OnSize are required for a container
  66. # when an object is being edited in-place.
  67. def OnSetFocus(self, msg):
  68. item = self.GetDocument().GetInPlaceActiveItem(self)
  69. if item is not None and item.GetItemState()==win32uiole.COleClientItem_activeUIState:
  70. wnd = item.GetInPlaceWindow()
  71. if wnd is not None:
  72. wnd.SetFocus()
  73. return 0 # Dont get the base version called.
  74. return 1 # Call the base version.
  75. def OnSize (self, params):
  76. item = self.GetDocument().GetInPlaceActiveItem(self)
  77. if item is not None:
  78. item.SetItemRects()
  79. return 1 # do call the base!
  80. class OleTemplate(docview.DocTemplate):
  81. def __init__(self, resourceId=None, MakeDocument=None, MakeFrame=None, MakeView=None):
  82. if MakeDocument is None: MakeDocument = OleDocument
  83. if MakeView is None: MakeView = ExcelView
  84. docview.DocTemplate.__init__(self, resourceId, MakeDocument, MakeFrame, MakeView)
  85. class WordFrame(window.MDIChildWnd):
  86. def __init__(self, doc = None):
  87. self._obj_ = win32ui.CreateMDIChild()
  88. self._obj_.AttachObject(self)
  89. # Dont call base class doc/view version...
  90. def Create(self, title, rect = None, parent = None):
  91. style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_OVERLAPPEDWINDOW
  92. self._obj_.CreateWindow(None, title, style, rect, parent)
  93. rect = self.GetClientRect()
  94. rect = (0,0,rect[2]-rect[0], rect[3]-rect[1])
  95. self.ocx = MyWordControl()
  96. self.ocx.CreateControl("Microsoft Word", win32con.WS_VISIBLE | win32con.WS_CHILD, rect, self, 20000)
  97. def Demo():
  98. import sys, win32api
  99. docName = None
  100. if len(sys.argv)>1:
  101. docName = win32api.GetFullPathName(sys.argv[1])
  102. OleTemplate().OpenDocumentFile(None)
  103. # f = WordFrame(docName)
  104. # f.Create("Microsoft Office")
  105. if __name__=='__main__':
  106. Demo()