c++调用python接口_Linux上使用Python调用WPS二次开发接口

e4d7c668f364e2f4007de2b8647fcb44.png

环境

  • Ubuntu18.04
  • WPS For Linux
  • Python3

简介

目前WPS for Linux已经支持二次开发。这次我们使用Python3来进行调用。目前 “社区的小伙伴”(我们团队的某个开发)把WPS的c++的接口封装成python的库了。上github的链接

https://github.com/timxx/pywpsrpc

这次使用这个库来进行调用。

流程

有两种方式来调用,一个是自己手动来,一个是用pip来安装这个库,本次使用pip库

sudo dpkg -i wps-office_11.1.0.9505_amd64.deb // 安装wps
pip3 install pywpsrpc -i https://pypi.tuna.tsinghua.edu.cn/simple

把库安装好,记得打开WPS,把WPS设置成多组件的模式 上面库有个demo,可以直接新建一个 1.py

# 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)

# use the RpcProxy to make things easy...
from pywpsrpc import RpcProxy

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

# all the calls returns the error code as the first value
# you can check it for fails
# 0 means all fines, you can use the common module's S_OK,
# FAILED or SUCCEEDED to check
# recommend use the RpcProxy instead

# get the application and you get everything...
# here we use the RpcProxy to wrap the application
# otherwise, you have to call Release on each instance
# and handle the hr for every call...
app = RpcProxy(rpc.getWpsApplication())

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

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

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

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

#def onDocumentBeforeSave(doc, saveAsUi, cancel):
    # to discard the saving, return cancel as True
 # return saveAsUi, cancel

# 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)

运行这个py

python3 ./1.py

然后会发现当前文件夹生成一个新的文档 test.docx.

更多的api可以上github去查看,当然什么保存成pdf就更没有问题了。

这个demo相当简单。代码更好懂了。你会发现API跟msdn的方法基本一致。这也是WPS的兼容mso的地方,不过目前应该只有WPS在Linux上有这种级别的API开放给大家。

5cde06fbf5a9b6bd98d7929fd6cdf93a.png

37f9e5f0925af79e04bc487ed2178bcb.gif

686e01432dc8691619c6f189f292b4bf.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值