Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
msoffice.py153 linesDownload Raw Back to ocx
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 7import regutil8import win32con9import win32ui10import win32uiole11from pywin.mfc import activex, docview, object, window12from win32com.client import gencache13 14# WordModule = gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 1033, 8, 0)15# if WordModule is None:16# 	raise ImportError, "Microsoft Word version 8 does not appear to be installed."17 18 19class OleClientItem(object.CmdTarget):20    def __init__(self, doc):21        object.CmdTarget.__init__(self, win32uiole.CreateOleClientItem(doc))22 23    def OnGetItemPosition(self):24        # For now return a hard-coded rect.25        return (10, 10, 210, 210)26 27    def OnActivate(self):28        # Allow only one inplace activate item per frame29        view = self.GetActiveView()30        item = self.GetDocument().GetInPlaceActiveItem(view)31        if item is not None and item._obj_ != self._obj_:32            item.Close()33        self._obj_.OnActivate()34 35    def OnChange(self, oleNotification, dwParam):36        self._obj_.OnChange(oleNotification, dwParam)37        self.GetDocument().UpdateAllViews(None)38 39    def OnChangeItemPosition(self, rect):40        # During in-place activation CEmbed_ExcelCntrItem::OnChangeItemPosition41        #  is called by the server to change the position of the in-place42        #  window.  Usually, this is a result of the data in the server43        #  document changing such that the extent has changed or as a result44        #  of in-place resizing.45        #46        # The default here is to call the base class, which will call47        #  COleClientItem::SetItemRects to move the item48        #  to the new position.49        if not self._obj_.OnChangeItemPosition(self, rect):50            return 051 52        # TODO: update any cache you may have of the item's rectangle/extent53        return 154 55 56class OleDocument(object.CmdTarget):57    def __init__(self, template):58        object.CmdTarget.__init__(self, win32uiole.CreateOleDocument(template))59        self.EnableCompoundFile()60 61 62class ExcelView(docview.ScrollView):63    def OnInitialUpdate(self):64        self.HookMessage(self.OnSetFocus, win32con.WM_SETFOCUS)65        self.HookMessage(self.OnSize, win32con.WM_SIZE)66 67        self.SetScrollSizes(win32con.MM_TEXT, (100, 100))68        rc = self._obj_.OnInitialUpdate()69        self.EmbedExcel()70        return rc71 72    def EmbedExcel(self):73        doc = self.GetDocument()74        self.clientItem = OleClientItem(doc)75        self.clientItem.CreateNewItem("Excel.Sheet")76        self.clientItem.DoVerb(-1, self)77        doc.UpdateAllViews(None)78 79    def OnDraw(self, dc):80        doc = self.GetDocument()81        pos = doc.GetStartPosition()82        clientItem, pos = doc.GetNextItem(pos)83        clientItem.Draw(dc, (10, 10, 210, 210))84 85    # Special handling of OnSetFocus and OnSize are required for a container86    #  when an object is being edited in-place.87    def OnSetFocus(self, msg):88        item = self.GetDocument().GetInPlaceActiveItem(self)89        if (90            item is not None91            and item.GetItemState() == win32uiole.COleClientItem_activeUIState92        ):93            wnd = item.GetInPlaceWindow()94            if wnd is not None:95                wnd.SetFocus()96            return 0  # Dont get the base version called.97        return 1  # Call the base version.98 99    def OnSize(self, params):100        item = self.GetDocument().GetInPlaceActiveItem(self)101        if item is not None:102            item.SetItemRects()103        return 1  # do call the base!104 105 106class OleTemplate(docview.DocTemplate):107    def __init__(108        self, resourceId=None, MakeDocument=None, MakeFrame=None, MakeView=None109    ):110        if MakeDocument is None:111            MakeDocument = OleDocument112        if MakeView is None:113            MakeView = ExcelView114        docview.DocTemplate.__init__(115            self, resourceId, MakeDocument, MakeFrame, MakeView116        )117 118 119class WordFrame(window.MDIChildWnd):120    def __init__(self, doc=None):121        self._obj_ = win32ui.CreateMDIChild()122        self._obj_.AttachObject(self)123        # Dont call base class doc/view version...124 125    def Create(self, title, rect=None, parent=None):126        style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_OVERLAPPEDWINDOW127        self._obj_.CreateWindow(None, title, style, rect, parent)128 129        rect = self.GetClientRect()130        rect = (0, 0, rect[2] - rect[0], rect[3] - rect[1])131        self.ocx = MyWordControl()132        self.ocx.CreateControl(133            "Microsoft Word", win32con.WS_VISIBLE | win32con.WS_CHILD, rect, self, 20000134        )135 136 137def Demo():138    import sys139 140    import win32api141 142    docName = None143    if len(sys.argv) > 1:144        docName = win32api.GetFullPathName(sys.argv[1])145    OleTemplate().OpenDocumentFile(None)146 147 148# 	f = WordFrame(docName)149# 	f.Create("Microsoft Office")150 151if __name__ == "__main__":152    Demo()153 
codekingpro/portable-devtools · Team Ai