def test_wps(): hr, rpc = createWpsRpcInstance() hr, app = rpc.getWpsApplication() visible = app.Visible print(visible) app.Visible = False print(app.Visible) app.Visible = True assert (app.Documents.Count == 0) try: doc = app.ActiveDocument assert (False) except AttributeError as e: print(e) try: app.ActiveDocument = None assert (False) except AttributeError as e: print(e) hr, doc = app.Documents.Add() assert (app.Documents.Count == 1) print(doc.Name) doc.Close() app.Quit()
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)
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) hr, app = rpc.getWpsApplication() if hr != S_OK: raise ConvertException("Can't get the application", hr) # we don't need the gui app.Visible = False docs = app.Documents def _handle_result(hr): if abort_on_fails and hr != S_OK: raise ConvertException("convert_file failed", hr) 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: hr = convert_file(file, docs, format) _handle_result(hr) else: hr = convert_file(abs_path, docs, format) _handle_result(hr) app.Quit()
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)
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()
def init(re_init): if re_init: subprocess.call("ps -ef|grep wps |awk '{print $2}'|xargs kill -9", shell=True) hr, rpc = createWpsRpcInstance() if hr != S_OK: raise ConvertException("Can't create the rpc instance", hr) hr, wps = rpc.getWpsApplication() if hr != S_OK: raise ConvertException("Can't get the application", hr) wps.Visible = False return wps.Documents
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()
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()
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()
def test(): hr, rpc = rpcwpsapi.createWpsRpcInstance() check_call("createWpsRpcInstance", hr, rpc) hr = rpc.setProcessArgs( [os.path.dirname(os.path.realpath(__file__)) + "/../pyproject.toml"]) check_call("setProcessArgs", hr) hr, wpsApp = rpc.getWpsApplication() check_call("rpc.getWpsApplication", hr, wpsApp) hr, pid = rpc.getProcessPid() check_call("getProcessPid", hr, pid) hr, docs = wpsApp.get_Documents() check_call("get_Documents", hr, docs) hr, count = docs.get_Count() check_call("get_Count", hr, count) hr, doc = wpsApp.get_ActiveDocument() check_call("get_ActiveDocument", hr, doc) hr, name = doc.get_Name() check_call("get_Name", hr, name) hr, docRange = doc.get_Content() check_call("get_Content", hr, docRange) hr, start = docRange.get_Start() check_call("get_Start", hr, start) hr, end = docRange.get_End() check_call("get_End", hr, end) hr, text = docRange.get_Text() check_call("get_Text", hr, text) # the text uses \r as newline, LoL print(text.replace("\r", "\n")) hr, paras = doc.get_Paragraphs() check_call("get_Paragraphs", hr, paras) hr, count = paras.get_Count() check_call("get_Count", hr, count) hr = docRange.InsertParagraphAfter() check_call("InsertParagraphAfter", hr) hr = docRange.InsertAfter("Hello, world") check_call("InsertAfter", hr) hr, count = paras.get_Count() check_call("get_Count", hr, count) hr, app2 = paras.get_Application() check_call("paras.get_Application", hr, app2) hr, build = app2.get_Build() check_call("app2.get_Build", hr, build) hr, para = paras.get_First() check_call("paras.get_First", hr, para) hr, dropCap = para.get_DropCap() check_call("para.get_DropCap", hr, dropCap) hr = dropCap.put_Position(wpsapi.wdDropNormal) check_call("dropCap.put_Position", hr) hr, paraRange = para.get_Range() check_call("para.get_Range", hr, paraRange) hr, text = paraRange.get_Text() check_call("paraRange.get_Text", hr, text) hr = para.put_Alignment(wpsapi.wdAlignParagraphCenter) check_call("para.put_Alignment", hr) hr, paraFormat = para.get_Format() check_call("para.get_Format", hr, paraFormat) hr, style = paraFormat.get_Style() check_call("paraFormat.get_Style", hr, style) hr, borders = paraFormat.get_Borders() check_call("paraFormat.get_Borders", hr, borders) hr, count = borders.get_Count() check_call("borders.get_Count", hr, count) hr = borders.put_Shadow(True) check_call("borders.put_Shadow", hr) hr = paraFormat.put_Borders(borders) check_call("paraFormat.put_Borders", hr) hr, shading = para.get_Shading() check_call("para.get_Shading", hr, shading) hr = shading.put_BackgroundPatternColor(wpsapi.wdColorGreen) check_call("shading.put_BackgroundPatternColor", hr) hr, tabStops = para.get_TabStops() check_call("para.get_TabStops", hr, tabStops) hr, count = tabStops.get_Count() check_call("tabStops.get_Count", hr, count) hr = docRange.put_Start(end) check_call("put_Start", hr) hr = docRange.put_End(end + 12) check_call("put_End", hr) hr, text = docRange.get_Text() # should be "Hello, world" check_call("get_Text", hr, text) # reset the range to find hr, docRange = doc.get_Content() hr, find = docRange.get_Find() check_call("docRange.get_Find", hr, find) hr, found = find.Execute("Hello, world", MatchWholeWord=True) check_call("find.Execute", hr, found) hr, font = docRange.get_Font() check_call("docRange.get_Font", hr, font) hr, name = font.get_Name() check_call("font.get_Name", hr, name) hr = font.put_Bold(True) check_call("font.put_Bold", hr) hr, selection = wpsApp.get_Selection() check_call("get_Selection", hr, selection) hr = selection.SetRange(0, 10) check_call("SetRange", hr) hr, start = selection.get_Start() check_call("get_Start", hr, start) hr, end = selection.get_End() check_call("get_End", hr, end) hr, text = selection.get_Text() check_call("get_Text", hr, text) hr, tables = doc.get_Tables() check_call("doc.get_Tables", hr, tables) hr, pos = selection.EndKey(Unit=wpsapi.wdStory) check_call("selection.EndKey", hr, pos) hr = selection.InsertParagraphAfter() hr, pos = selection.EndKey(Unit=wpsapi.wdStory) hr, selRange = selection.get_Range() check_call("selection.get_Range", hr, selRange) hr, table = tables.Add(selRange, 2, 2) check_call("tables.Add", hr, table) hr, cell = table.Cell(1, 2) check_call("table.Cell", hr, cell) hr, cellRange = cell.get_Range() check_call("cell.get_Range", hr, cellRange) hr = cellRange.put_Text("cell") check_call("cellRange.put_Text", hr) hr, shapes = doc.get_Shapes() check_call("doc.get_Shapes", hr, shapes) hr, shape = shapes.AddShape(wpsapi.msoShapeMoon, 0, 0, 100, 100) check_call("shapes.AddShape", hr, shape) hr = shape.put_AlternativeText("this is a moon shape") check_call("shape.put_AlternativeText", hr) hr, fields = doc.get_Fields() check_call("doc.get_Fields", hr, fields) hr, field = fields.Add(selRange, Type=wpsapi.wdFieldDate) check_call("fields.Add", hr, field) hr, bookmarks = doc.get_Bookmarks() check_call("doc.get_Bookmarks", hr, bookmarks) hr, bkmRange = doc.Range(0, 10) check_call("doc.Range", hr, bkmRange) hr, bookmark = bookmarks.Add("BookmarkName", bkmRange) check_call("bookmarks.Add", hr, bookmark) hr, count = bookmarks.get_Count() check_call("bookmarks.get_Count", hr, count) hr, name = bookmark.get_Name() check_call("bookmark.get_Name", hr, name) hr, tocs = doc.get_TablesOfContents() check_call("doc.get_TablesOfContents", hr, tocs) hr, bkmRange = bookmark.get_Range() check_call("bookmark.get_Range", hr, bkmRange) hr, toc = tocs.Add(bkmRange) check_call("tocs.Add", hr, toc) hr = doc.SaveAs("test.docx", wpsapi.wdFormatDocument) check_call("SaveAs", hr) hr = doc.Close() check_call("Close", hr) hr, doc = docs.Open("test.docx", ReadOnly=True) check_call("Open", hr, doc) hr, readOnly = doc.get_ReadOnly() check_call("get_ReadOnly", hr, readOnly) hr, window = doc.get_ActiveWindow() check_call("doc.get_ActiveWindow", hr, window) hr, caption = window.get_Caption() check_call("window.get_Caption", hr, caption) doc.Close() check_call("Close", hr) hr, doc = docs.Add(Visible=False) check_call("Add", hr, doc) hr, name = doc.get_Name() check_call("get_Name", hr, name) hr, name = wpsApp.get_Name() check_call("get_Name", hr, name) hr, version = wpsApp.get_Version() check_call("get_Version", hr, version) hr, build = wpsApp.get_Build() check_call("get_Build", hr, build) hr, caption = wpsApp.get_Caption() check_call("get_Caption", hr, caption) hr = wpsApp.put_Caption("test标题") check_call("put_Caption", hr) hr, caption = wpsApp.get_Caption() check_call("get_Caption", hr, caption) hr = wpsApp.put_Visible(False) check_call("put_Visible", hr) hr, visible = wpsApp.get_Visible() check_call("get_Visible", hr, visible) hr = wpsApp.put_Visible(True) check_call("put_Visible", hr) hr, visible = wpsApp.get_Visible() check_call("get_Visible", hr, visible) hr = wpsApp.Quit(wpsapi.wdDoNotSaveChanges) check_call("Quit", hr)
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()