Example #1
0
def main():
    hr, rpc = createWpsRpcInstance()
    if FAILED(hr):
        print("createWpsRpcInstance failed with hr: ", hr)
        sys.exit(-1)

    app = RpcProxy(rpc.getWpsApplication())
    print(app.last_error)

    # get the raw object
    obj = app.rpc_object
    print(obj)

    print(app.Build)
    app.Visible = False
    app.Visible = True

    doc = app.Documents.Add()
    print(doc.Name)

    if not app.Selection.InsertAfter("Hello, world"):
        print("Can't insert text")

    font = app.Selection.Font
    font.Bold = True

    doc.Close(SaveChanges=wpsapi.wdDoNotSaveChanges)

    try:
        # should be failed since no Documents opened.
        doc = app.ActiveDocument
    except Exception as e:
        print(e)

    app.Quit(wpsapi.wdDoNotSaveChanges)
Example #2
0
    def _onOpenWps(self):
        if self._wpsApp:
            return

        hr, rpc = createWpsRpcInstance()
        if FAILED(hr):
            QMessageBox.critical(
                self, "Open Wps", "Failed to call createWpsRpcInstance")
            return

        args = ["-shield", "-multiply", "-x11embed",
                "{}".format(self._wpsWnd.winId()),
                "{}".format(self._wpsWnd.width()),
                "{}".format(self._wpsWnd.height())
                ]

        rpc.setProcessArgs(args)

        hr, self._wpsApp = rpc.getWpsApplication()
        if not self._wpsApp:
            QMessageBox.critical(
                self, "Open Wps", "Failed to call getWpsApplication")
            return

        appEx = self._wpsApp.ApplicationEx
        if appEx:
            container = QWidget.createWindowContainer(
                QWindow.fromWinId(appEx.EmbedWid), self)
            self._wpsWnd.setContainer(container)
            # FIXME: the container isn't place correctly
            if not self.isMaximized():
                self.resize(self.width() + 1, self.height())

        self.btnOpenDoc.setEnabled(True)
Example #3
0
def test_rpcwpsapi():
    try:
        from pywpsrpc.rpcwpsapi import (createWpsRpcInstance, wpsapi)
    except ImportError:
        return
    hr, rpc = createWpsRpcInstance()
    if FAILED(hr):
        print("createWpsRpcInstance failed with hr: ", hr)
        sys.exit(-1)

    app = RpcProxy(rpc.getWpsApplication())

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "DocumentBeforeClose", _onDocumentBeforeClose)
    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "DocumentBeforeSave", _onDocumentBeforeSave)

    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "DocumentChange", _onDocumentChange)

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "DocumentOpen", _onDocumentOpen)

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "NewDocument", _onNewDocument)

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "Quit", _onQuit)

    doc = app.Documents.Add()
    # the doc should not be saved
    doc.SaveAs2("test.doc")

    # DocumentChange
    doc2 = app.Documents.Add()

    doc3 = app.Documents.Open(os.path.realpath(__file__))

    # the doc should not be closed
    doc.Close()

    # now make it works
    global can_close_doc
    can_close_doc = True

    app.Quit()
Example #4
0
def test_rpcetapi():
    try:
        from pywpsrpc.rpcetapi import (createEtRpcInstance, etapi)
    except ImportError:
        return

    hr, rpc = createEtRpcInstance()
    if FAILED(hr):
        print("createEtRpcInstance failed with hr: ", hr)
        sys.exit(-1)

    app = RpcProxy(rpc.getEtApplication())

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents,
                           "WorkbookBeforeClose", _onWorkbookBeforeClose)
    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents,
                           "WorkbookBeforeSave", _onWorkbookBeforeSave)

    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents,
                           "WorkbookAfterSave", _onWorkbookAfterSave)

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents, "NewWorkbook",
                           _onNewWorkbook)

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents,
                           "WorkbookOpen", _onWorkbookOpen)

    wb = app.Workbooks.Add()
    # the doc should not be saved
    wb.SaveAs("test.xls")
    # the doc should not be closed
    wb.Close()

    # now make it works
    global can_close_wb
    can_close_wb = True

    # save again
    wb.SaveAs("test2.xls")
    wb.Close()

    wb = app.Workbooks.Open("test2.xls")

    app.Quit()
Example #5
0
    def _onOpenDocument(self):
        if not self._wpsApp:
            return

        filePath, _ = QFileDialog.getOpenFileName(
            self, "Select File",
            QStandardPaths.standardLocations(
                QStandardPaths.DocumentsLocation)[0])

        if not filePath:
            return

        hr, _ = self._wpsApp.Documents.Open(filePath)
        if FAILED(hr):
            QMessageBox.critical(
                self, "Demo", "Failed to call open document '%s'" % filePath)
Example #6
0
def test_rpcwppapi():
    try:
        from pywpsrpc.rpcwppapi import (createWppRpcInstance, wppapi)
    except ImportError:
        return

    hr, rpc = createWppRpcInstance()
    if FAILED(hr):
        print("createWppRpcInstance failed with hr: ", hr)
        sys.exit(-1)

    app = RpcProxy(rpc.getWppApplication())

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "PresentationSave", _onPresentationSave)
    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "PresentationClose", _onPresentationClose)

    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "AfterNewPresentation", _onAfterNewPresentation)

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "AfterPresentationOpen", _onAfterPresentationOpen)

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "NewPresentation", _onNewPresentation)

    appEx = app.ApplicationEx
    hr = rpc.registerEvent(appEx.rpc_object, wpsapiex.DIID_ApplicationEventsEx,
                           "DocumentAfterPrint", _onDocumentAfterPrint)

    pres = app.Presentations.Add(wppapi.msoTrue)
    pres.SaveAs("test.ppt")
    pres.Close()

    pres = app.Presentations.Open("test.ppt", wppapi.msoFalse, wppapi.msoFalse,
                                  wppapi.msoTrue)

    pres.PrintOut()

    app.Quit()
Example #7
0
def test_rpcwpsapi():
    try:
        from pywpsrpc.rpcwpsapi import (createWpsRpcInstance, wpsapi)
    except ImportError:
        return
    hr, rpc = createWpsRpcInstance()
    if FAILED(hr):
        print("createWpsRpcInstance failed with hr: ", hr)
        sys.exit(-1)

    app = RpcProxy(rpc.getWpsApplication())

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "DocumentBeforeClose", _onDocumentBeforeClose)
    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "DocumentBeforeSave", _onDocumentBeforeSave)

    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "DocumentChange", _onDocumentChange)

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "DocumentOpen", _onDocumentOpen)

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "NewDocument", _onNewDocument)

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "Quit", _onQuit)

    def _onWindowActivate(doc, window):
        ss = {
            wpsapi.wdWindowStateNormal: "wdWindowStateNormal",
            wpsapi.wdWindowStateMaximize: "wdWindowStateMaximize",
            wpsapi.wdWindowStateMinimize: "wdWindowStateMinimize"
        }
        print("_onWindowActivate:", doc.Name, window.Caption,
              ss[window.WindowState])

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "WindowActivate", _onWindowActivate)

    def _onWindowDeactivate(doc, window):
        print("_onWindowDeactivate:", window.Caption)

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "WindowDeactivate", _onWindowDeactivate)

    def _onWindowSelectionChange(selection):
        print("_onWindowSelectionChange:", selection.Start, selection.End,
              selection.Text)

    hr = rpc.registerEvent(app.rpc_object, wpsapi.DIID_ApplicationEvents4,
                           "WindowSelectionChange", _onWindowSelectionChange)

    doc = app.Documents.Add()
    # the doc should not be saved
    doc.SaveAs2("test.doc")

    # DocumentChange
    doc2 = app.Documents.Add()

    doc3 = app.Documents.Open(os.path.realpath(__file__))

    selection = app.Selection
    # selection should changed
    selection.SetRange(0, 10)

    # the doc should not be closed
    doc.Close()

    # now make it works
    global can_close_doc
    can_close_doc = True

    app.Quit()
Example #8
0
def test_rpcetapi():
    try:
        from pywpsrpc.rpcetapi import (createEtRpcInstance, etapi)
    except ImportError:
        return

    hr, rpc = createEtRpcInstance()
    if FAILED(hr):
        print("createEtRpcInstance failed with hr: ", hr)
        sys.exit(-1)

    app = RpcProxy(rpc.getEtApplication())

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents,
                           "WorkbookBeforeClose", _onWorkbookBeforeClose)
    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents,
                           "WorkbookBeforeSave", _onWorkbookBeforeSave)

    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents,
                           "WorkbookAfterSave", _onWorkbookAfterSave)

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents, "NewWorkbook",
                           _onNewWorkbook)

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents,
                           "WorkbookOpen", _onWorkbookOpen)

    def _onWindowActivate(wb, window):
        ss = {
            etapi.xlMaximized: "xlMaximized",
            etapi.xlMinimized: "xlMinimized",
            etapi.xlNormal: "xlNormal"
        }
        print("_onWindowActivate:", window.Caption, ss[window.WindowState])

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents,
                           "WindowActivate", _onWindowActivate)

    def _onWindowDeactivate(wb, window):
        print("_onWindowDeactivate:", wb.Name, window.Caption)

    hr = rpc.registerEvent(app.rpc_object, etapi.DIID_AppEvents,
                           "WindowDeactivate", _onWindowDeactivate)

    wb = app.Workbooks.Add()
    # the doc should not be saved
    wb.SaveAs("test.xls")
    # the doc should not be closed
    wb.Close()

    # now make it works
    global can_close_wb
    can_close_wb = True

    # save again
    wb.SaveAs("test2.xls")
    wb.Close()

    wb = app.Workbooks.Open("test2.xls")

    app.Quit()
Example #9
0
def test_rpcwppapi():
    try:
        from pywpsrpc.rpcwppapi import (createWppRpcInstance, wppapi)
    except ImportError:
        return

    hr, rpc = createWppRpcInstance()
    if FAILED(hr):
        print("createWppRpcInstance failed with hr: ", hr)
        sys.exit(-1)

    app = RpcProxy(rpc.getWppApplication())

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "PresentationSave", _onPresentationSave)
    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "PresentationClose", _onPresentationClose)

    print("registerEvent:", hex(hr & 0xFFFFFFFF))

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "AfterNewPresentation", _onAfterNewPresentation)

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "AfterPresentationOpen", _onAfterPresentationOpen)

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "NewPresentation", _onNewPresentation)

    def _onWindowActivate(doc, window):
        ss = {
            wppapi.ppWindowNormal: "ppWindowNormal",
            wppapi.ppWindowMinimized: "ppWindowMinimized",
            wppapi.ppWindowMaximized: "ppWindowMaximized"
        }
        print("_onWindowActivate:", window.Caption, ss[window.WindowState])

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "WindowActivate", _onWindowActivate)

    def _onWindowDeactivate(doc, window):
        print("_onWindowDeactivate:", window.Caption)

    hr = rpc.registerEvent(app.rpc_object, wppapi.IID_EApplication,
                           "WindowDeactivate", _onWindowDeactivate)

    appEx = app.ApplicationEx
    hr = rpc.registerEvent(appEx.rpc_object, wpsapiex.DIID_ApplicationEventsEx,
                           "DocumentAfterPrint", _onDocumentAfterPrint)

    pres = app.Presentations.Add(wppapi.msoTrue)
    pres.SaveAs("test.ppt")
    pres.Close()

    pres = app.Presentations.Open("test.ppt", wppapi.msoFalse, wppapi.msoFalse,
                                  wppapi.msoTrue)

    pres.PrintOut()

    app.Quit()