Пример #1
0
def test_wps():
    hr, rpc = createWpsRpcInstance()
    app = RpcProxy(rpc.getWpsApplication())
    docs = app.Documents

    docs.Add()
    print(len(docs))

    docs.Add()
    print(len(docs))

    # index starts from 1, not 0
    for i in range(1, len(docs) + 1):
        print("index.docName: ", docs[i].Name)

    for doc in docs:
        print("iter.docName: ", doc.Name)

    # use raw object
    for doc in RpcIter(docs.rpc_object):
        print("iter2.docName: ", doc.Name)

    # access by name
    name = docs[2].Name
    doc = docs[name]
    print("doc", doc.Name)

    doc = docs.Open(os.path.dirname(os.path.realpath(__file__)) +
                    "/../pyproject.toml",
                    ReadOnly=True)
    for para in doc.Paragraphs:
        print(para.Range.Text)

    app.Quit()
Пример #2
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)
Пример #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()
Пример #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()
Пример #5
0
def test_et():
    hr, rpc = createEtRpcInstance()
    app = RpcProxy(rpc.getEtApplication())

    wbs = app.Workbooks
    print(len(wbs))

    wbs.Add()
    print(len(wbs))

    wbs.Add()
    print(len(wbs))

    for wb in wbs:
        print(wb.Name)

    app.Quit()
Пример #6
0
def test_wpp():
    hr, rpc = createWppRpcInstance()
    app = RpcProxy(rpc.getWppApplication())

    preses = app.Presentations
    print(len(preses))

    preses.Add()
    print(len(preses))

    preses.Add()
    print(len(preses))

    for pres in preses:
        print(pres.Name)

    app.Quit()
Пример #7
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()
Пример #8
0
def convert_to(paths, format, abort_on_fails=False):
    hr, rpc = createWpsRpcInstance()
    if hr != S_OK:
        raise ConvertException("Can't create the rpc instance", hr)

    app = RpcProxy(rpc.getWpsApplication(), True)

    # we don't need the gui
    app.Visible = False

    docs = app.Documents
    docs.use_exception = abort_on_fails

    for path in paths:
        abs_path = os.path.realpath(path)
        if os.path.isdir(abs_path):
            files = [(os.path.join(abs_path, f)) for f in os.listdir(abs_path)]
            for file in files:
                convert_file(file, docs, format)
        else:
            convert_file(abs_path, docs, format)

    app.Quit()
Пример #9
0
def test_wps():
    hr, rpc = createWpsRpcInstance()
    app = RpcProxy(rpc.getWpsApplication())
    docs = app.Documents

    docs.Add()
    print(len(docs))

    docs.Add()
    print(len(docs))

    # index starts from 1, not 0
    for i in range(1, len(docs) + 1):
        print("index.docName: ", docs[i].Name)

    for doc in docs:
        print("iter.docName: ", doc.Name)

    # access by name
    name = docs[2].Name
    doc = docs[name]
    print("doc", doc.Name)

    app.Quit()
Пример #10
0
class MyWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self._wpsApp = None

        layout = QVBoxLayout(self)

        hbox = QHBoxLayout()
        layout.addLayout(hbox)

        btnOpen = QPushButton("Open Wps", self)
        btnOpen.clicked.connect(self._onOpenWps)
        hbox.addWidget(btnOpen)

        self.btnOpenDoc = QPushButton("Open Document", self)
        self.btnOpenDoc.clicked.connect(self._onOpenDocument)
        self.btnOpenDoc.setEnabled(False)
        hbox.addWidget(self.btnOpenDoc)

        hbox.addStretch()

        self._wpsWnd = WpsWindow(self)
        layout.addWidget(self._wpsWnd)

    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)

        self._wpsApp = RpcProxy(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)

    def _onOpenDocument(self):
        if not self._wpsApp:
            return

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

        if not filePath:
            return

        doc = self._wpsApp.Documents.Open(filePath)
        if not doc:
            QMessageBox.critical(
                self, "Demo", "Failed to call open document '%s'" % filePath)

    def closeEvent(self, event):
        if self._wpsApp:
            self._wpsApp.Quit()
        event.accept()
Пример #11
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()
Пример #12
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()
Пример #13
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()