Skip to main content

Python bindings for the WPS Office RPC

Project description

pywpsrpc

Build Status PyPI version Python version License: MIT

WPS Office for Linux二次开发C++接口Python绑定.

Read this in other languages: English

WPS Office二次开发接口

WPS二次开发接口允许开发者通过给定的接口来调起WPS并执行指定的任务, 比如你可以打开、编辑然后保存文档,转换文档格式等文档自动化操作,又或者将WPS嵌入到自己的程序、进行功能扩展等。

关于详细的接口文档,可参考

对于pywpsrpc本身的文档,可跳转到wiki查看。

依赖

  • Python 3.6+
  • Qt5 (C++ SDK依赖)
  • WPS Office for Linux 11.1.0.9080+
  • sip 6.0+ (仅编译)
  • qmake (仅编译)
  • g++ (仅编译)
  • 桌面环境(WPS运行需要)

PS,目前WPS整合模式有BUG,非把WPS嵌入到自己进程的场景建议先切换到多组件模式

如何编译

在项目顶层目录下运行终端执行: sip-build

如果需要查看编译相关输出信息,可添加 --verbose 选项

安装

建议通过PyPi安装:

pip install pywpsrpc

如果默认的源太慢,建议自行搜索添加中国pip源

通过源码编译安装:

先打包执行: sip-wheel

打包完成后再通过pip install pywpsrpc-*.whl命令安装

快速上手

# 第一步先import所需模块(rpcxxxapi,xxx为对应项目的名字)
# rpcwpsapi模块为WPS文字项目的开发接口
# rpcwppapi则是WPS演示的
# rpcetapi毫无疑问就是WPS表格的了
# 另外还有common模块,为前三者的公共接口模块,通常不能单独使用

# 调起WPS必需通过createXXXRpcInstance接口,所以导入它是必需的
# 以WPS文字为例
from pywpsrpc.rpcwpsapi import (createWpsRpcInstance, wpsapi)
from pywpsrpc import RpcIter


# 这里仅创建RPC实例
hr, rpc = createWpsRpcInstance()

# 注意:
# WPS开发接口的返回值第一个总是HRESULT(无返回值的除外)
# 通常不为0的都认为是调用失败(0 == common.S_OK)
# 可以使用common模块里的FAILED或者SUCCEEDED去判断

# 通过rpc实例调起WPS进程
hr, app = rpc.getWpsApplication()

# 比如添加一个空白文档
hr, doc = app.Documents.Add()

# 加点文字
selection = app.Selection
selection.InsertAfter("Hello, world")

# 将前面插入的"Hello, world"加粗
selection.Font.Bold = True

# 光标移到末尾
selection.EndKey()

# 再插入空段
selection.InsertParagraph()

# 光标移到新段
selection.MoveDown()

# 再码些文字
selection.TypeText("pywpsrpc~")

# 使用RpcIter遍历所有段
paras = doc.Paragraphs
for para in RpcIter(paras):
    print(para.Range.Text)

# 或者通过索引方式
for i in range(0, paras.Count):
    # 注意:首个元素总是从1开始
    print(paras[i + 1].OutlineLevel)

# 也支持VBA风格的call语法访问集合元素
first_para = paras(1)
print(first_para.Range.Text)

def onDocumentBeforeSave(doc):
    # 如果想取消当前文档保存,第二个返回值设为True
    print("onDocumentBeforeSave called for doc: ", doc.Name)
    # SaveAsUI, Cancel
    return True, False

# 注册文档保存前通知
rpc.registerEvent(app,
                  wpsapi.DIID_ApplicationEvents4,
                  "DocumentBeforeSave",
                  onDocumentBeforeSave)

# 保存文档, onDocumentBeforeSave会被调用到
doc.SaveAs2("test.docx")

# 退出WPS进程
# 使用wpsapi.wdDoNotSaveChanges来忽略文档改动
app.Quit(wpsapi.wdDoNotSaveChanges)

例子

文档互转

嵌入

WPS嵌入第三方进程窗口

在服务器上运行

点我查看

关于授权

pywpsrpc为MIT开源授权协议,项目本身允许商用,但前提是你所使用的WPS Office For Linux版本允许商用(目前个人版本即社区版本不允许),具体需要联系WPS官方咨询。对于不在合理范围使用本项目的,本项目不承担任何法律责任。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pywpsrpc-2.4.0.tar.gz (10.2 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pywpsrpc-2.4.0-cp314-cp314-manylinux_2_5_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.5+ x86-64

pywpsrpc-2.4.0-cp313-cp313-manylinux_2_5_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.5+ x86-64

pywpsrpc-2.4.0-cp312-cp312-manylinux_2_5_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.5+ x86-64

pywpsrpc-2.4.0-cp311-cp311-manylinux_2_5_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.5+ x86-64

pywpsrpc-2.4.0-cp310-cp310-manylinux_2_5_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.5+ x86-64

pywpsrpc-2.4.0-cp39-cp39-manylinux_2_5_x86_64.whl (47.5 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.5+ x86-64

pywpsrpc-2.4.0-cp38-cp38-manylinux_2_5_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.5+ x86-64

pywpsrpc-2.4.0-cp37-cp37m-manylinux1_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.7m

File details

Details for the file pywpsrpc-2.4.0.tar.gz.

File metadata

  • Download URL: pywpsrpc-2.4.0.tar.gz
  • Upload date:
  • Size: 10.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pywpsrpc-2.4.0.tar.gz
Algorithm Hash digest
SHA256 f2af3b0c4d72f37592be16b3ced41b5bae366cb0fc4e23165d7ffb3457d80b7c
MD5 15a83546c1252e62a809ad2f6e84f887
BLAKE2b-256 b3be261ad7f6ca52c1b1f439b916e0e215add7ec9990b3a5349f91b374361c0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pywpsrpc-2.4.0.tar.gz:

Publisher: main.yml on timxx/pywpsrpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pywpsrpc-2.4.0-cp314-cp314-manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pywpsrpc-2.4.0-cp314-cp314-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 0bfe103d7c8523d4b568f97990563ffaecb3f2b19d95470c2d926838e1296975
MD5 939099aa4003af276da57591b02393e4
BLAKE2b-256 3911e78db17521f6257fa164a892e269fc43cf4f2be1bc6d625c4d299e404070

See more details on using hashes here.

Provenance

The following attestation bundles were made for pywpsrpc-2.4.0-cp314-cp314-manylinux_2_5_x86_64.whl:

Publisher: main.yml on timxx/pywpsrpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pywpsrpc-2.4.0-cp313-cp313-manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pywpsrpc-2.4.0-cp313-cp313-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 49bfbfcbbef59b7ec6c0c2aa36f96588a3d13a09c4d37c5acfdd50cc7758ed66
MD5 a47b2183299381d1a91433a6a6171f6e
BLAKE2b-256 96bf3bb09265bfbb96a72a90627177be3a405e85fdc5edb0f407807035971b9d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pywpsrpc-2.4.0-cp313-cp313-manylinux_2_5_x86_64.whl:

Publisher: main.yml on timxx/pywpsrpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pywpsrpc-2.4.0-cp312-cp312-manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pywpsrpc-2.4.0-cp312-cp312-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 4bb25edb4e33a1175701c71bbf0e8bf3a00f09e7cfbc85f0b7ccd9ebe24402e0
MD5 c91e07f6d569c6b882cd4ade9223e087
BLAKE2b-256 d574612f60013e9e5b3625968cc219949107c67b51b09e7cd2ed53d0f0e5e1d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pywpsrpc-2.4.0-cp312-cp312-manylinux_2_5_x86_64.whl:

Publisher: main.yml on timxx/pywpsrpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pywpsrpc-2.4.0-cp311-cp311-manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pywpsrpc-2.4.0-cp311-cp311-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 88c1a09c1a6a3274735b389982c50486b9b610ccb8dc6cb262c78bbb7e13d94a
MD5 2e81750643349cf9a33f0e514ee59ea4
BLAKE2b-256 b1399448d386d4e919156d9b79bafeb27b564094c7d8906d43301be0c01678c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pywpsrpc-2.4.0-cp311-cp311-manylinux_2_5_x86_64.whl:

Publisher: main.yml on timxx/pywpsrpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pywpsrpc-2.4.0-cp310-cp310-manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pywpsrpc-2.4.0-cp310-cp310-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 7f6d84fee2f65c73ab015d314431dd9d34c33c3c62c57471586a138ebf339618
MD5 63b998c8fbbcf79316437e7000db0693
BLAKE2b-256 e9507f9053948c7a67d660b4030281b053bae9df370cb669d69c1b4fe6bb57c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pywpsrpc-2.4.0-cp310-cp310-manylinux_2_5_x86_64.whl:

Publisher: main.yml on timxx/pywpsrpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pywpsrpc-2.4.0-cp39-cp39-manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pywpsrpc-2.4.0-cp39-cp39-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 0fe4e996ae6632dc819c3113cace0804beeee2db92a393cc19b0317bd456d7aa
MD5 edfd53004afdb29836354803184c3d0e
BLAKE2b-256 bb6f24951216401ce3f3f9c1b502affa082251b386f75aff8617c452401d328c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pywpsrpc-2.4.0-cp39-cp39-manylinux_2_5_x86_64.whl:

Publisher: main.yml on timxx/pywpsrpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pywpsrpc-2.4.0-cp38-cp38-manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pywpsrpc-2.4.0-cp38-cp38-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 a2e78c05f8a4882358ea85795c62fd54163732537f9718aa55781be2e725e2d2
MD5 fb8e678e51c6fd8b2f471c99db4e0876
BLAKE2b-256 bc9ca6239631acc03bd1b120cd9c71acd2c175ce2bdbf9e6a8747a45143ee8a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pywpsrpc-2.4.0-cp38-cp38-manylinux_2_5_x86_64.whl:

Publisher: main.yml on timxx/pywpsrpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pywpsrpc-2.4.0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for pywpsrpc-2.4.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5be044643c12a1a44be12ff9eba5895685195df9fe298b18ba06a2e74ddc780f
MD5 5c720832196651926eebd23928d0837f
BLAKE2b-256 4cc1dc501ea28c1ec76e33612c14e5c296e111e7f44078b0f7f688cea5d29e0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pywpsrpc-2.4.0-cp37-cp37m-manylinux1_x86_64.whl:

Publisher: main.yml on timxx/pywpsrpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page