Skip to content

Latest commit

 

History

History
148 lines (101 loc) · 4.15 KB

README_en.md

File metadata and controls

148 lines (101 loc) · 4.15 KB

pywpsrpc

Build Status PyPI version Python version License: MIT

Python bindings for WPS Office RPC (for Linux version only).

Read this in other languages: 简体中文.

What is WPS Office RPC?

To make it simple, it means you can use the rpc (api) get working with WPS Office, open, edit and save documents, extend the WPS Office functionals etc.

For full API references, go to

For pywpsrpc document, go to the wiki page.

Requirements

  • Python 3.6+
  • Qt5 (the SDK required both for building & running)
  • WPS Office for Linux 11.1.0.9080+
  • sip 6.0+ (for building)
  • qmake (for building)
  • g++ (for building)
  • Desktop Environment (WPS Office required)

If you don't embed WPS into your program, you should switch WPS to All-in-One mode.

How to build

Run sip-build under the project root directory

append --verbose for getting the progress of building

Installation

The pywpsrpc can be installed from PyPi:

pip install pywpsrpc

You can also install from the source:

Run sip-wheel package first, and then install the whl by pip install

Quick Start

# First import the module you want
# rpcwpsapi contains the interfaces for WPS
# rpcwppapi is for WPP
# and rpcetapi for ET
# the common module contains the shared interfaces, you can not use it alone.

# you always need the createXXXRpcInstance, so first import
# take wps for example here
from pywpsrpc.rpcwpsapi import (createWpsRpcInstance, wpsapi)
from pywpsrpc import RpcIter


# now create the rpc instance
hr, rpc = createWpsRpcInstance()

# most of the call returns the error code as the first value
# you can check it for fails (negative number means failed)
# 0 means all fines, you can use the common module's S_OK,
# FAILED or SUCCEEDED to check

# get the application and you get everything...
hr, app = rpc.getWpsApplication()

# Add blank doc e.g.
hr, doc = app.Documents.Add()

# append text...
selection = app.Selection
selection.InsertAfter("Hello, world")

# bold the "Hello, world"
selection.Font.Bold = True

# move cursor to end
selection.EndKey()

# add new paragraph
selection.InsertParagraph()

# move to next paragraph
selection.MoveDown()

# type some text
selection.TypeText("pywpsrpc~")

# iterate all paragraphs
paras = doc.Paragraphs
for para in RpcIter(paras):
    print(para.Range.Text)

# or use index based to loop
for i in range(0, paras.Count):
    # the first element starts with 1
    print(paras[i + 1].OutlineLevel)

def onDocumentBeforeSave(doc):
    # to discard the saving, return Cancel as True
    print("onDocumentBeforeSave called for doc: ", doc.Name)
    # SaveAsUI, Cancel
    return True, False

# get a notify about saving
rpc.registerEvent(app,
                  wpsapi.DIID_ApplicationEvents4,
                  "DocumentBeforeSave",
                  onDocumentBeforeSave)

# save the doc, onDocumentBeforeSave will be called
doc.SaveAs2("test.docx")

# Quit the application if you don't need anymore
# use wpsapi.wdDoNotSaveChanges to ignore the changes
app.Quit(wpsapi.wdDoNotSaveChanges)

Examples

Document Format Converter

Embedded

Embed WPS into self process

Run on Server

Click here

About License

It's free to use pywpsrpc even for commercial as MIT allows, but you must ensure your WPS Office for Linux allows you to do so. Regarding the use of this project outside reasonable bounds, this project assumes no legal liability.