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)

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嵌入第三方进程窗口

在服务器上运行

点我查看

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.3.5.tar.gz (6.1 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.3.5-cp311-cp311-manylinux_2_5_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.5+ x86-64

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

Uploaded CPython 3.10manylinux: glibc 2.5+ x86-64

pywpsrpc-2.3.5-cp39-cp39-manylinux_2_5_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.5+ x86-64

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

Uploaded CPython 3.8manylinux: glibc 2.5+ x86-64

pywpsrpc-2.3.5-cp37-cp37m-manylinux_2_5_x86_64.whl (8.0 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.5+ x86-64

pywpsrpc-2.3.5-cp36-cp36m-manylinux1_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.6m

File details

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

File metadata

  • Download URL: pywpsrpc-2.3.5.tar.gz
  • Upload date:
  • Size: 6.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.15 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for pywpsrpc-2.3.5.tar.gz
Algorithm Hash digest
SHA256 11c9ad0f75d032f2881830ef6b6ae73fa70a7119d1ebe77b235e3c54c18095bc
MD5 7b43f42a6f2f1e129a6aed59f139e690
BLAKE2b-256 315265140e9d1c5c621546c341e2d1c56d1781f34d99dd3cf717cc04cf63a9e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pywpsrpc-2.3.5-cp311-cp311-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 3244bd93e522ce07444c91981d0d3b7982053be340a1973ddd112fa30c5d462a
MD5 0702a6c8c1f62f034ccc6fe4d558e99a
BLAKE2b-256 62219ce0f1a0a96fe694182ea27df580866b12f7b0ed60fff90fa5f53faf18d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pywpsrpc-2.3.5-cp310-cp310-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 db79c3c1c167de9cdda0f29aaa2e3a750b8ba81793ca1733983ff98b9b9fd972
MD5 0d754640125f40d18f3d1c6a0d5af278
BLAKE2b-256 23958d6d3895f7fa06358f2e8a6913098c2e6324f7edd8b48545962f495098a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pywpsrpc-2.3.5-cp39-cp39-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 b54c79f84b0ac7c29c21fc75c6cd93b23222b6d5f2ced704cef4c6ddfaf98870
MD5 9c175948be1ed337ac99d121411e9bea
BLAKE2b-256 9cf73035fcf33bd1aa48789ae582288e81bdb895f93cc11d575cc60a88ddeb60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pywpsrpc-2.3.5-cp38-cp38-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 ce08935118154147eb93d756b88ec3ba2218e107407935665dc11454d98afd11
MD5 b7813e7ec22ad5c5f44380020be914d7
BLAKE2b-256 9c8df7125b7fda887d4ed38bd7e9bb78a1a5e419a5221246a6f6eef7759ee85f

See more details on using hashes here.

File details

Details for the file pywpsrpc-2.3.5-cp37-cp37m-manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pywpsrpc-2.3.5-cp37-cp37m-manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 6dd9e33a36ad34014330091390ceda7eaec27a79afa2d633c826951ab2bbf8fb
MD5 ea94dfee1ab1ff86108b1cc5f9fef3cd
BLAKE2b-256 42b328088b4431c9371f3df773ecb375a277b20e9ed8295e7e4885223ab4aa14

See more details on using hashes here.

File details

Details for the file pywpsrpc-2.3.5-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: pywpsrpc-2.3.5-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 7.9 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.15 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for pywpsrpc-2.3.5-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 703d25981afc42503923830559a6eeda2953600e85d9149396053bed122fed38
MD5 d1420ab5f23833f924356fe6b71ab976
BLAKE2b-256 bea3241d29b4b46f23beb164b320608f766624459106b88469c8086a454fe19f

See more details on using hashes here.

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