libreoffice python SDK使用教程(包括插入svg矢量图)

部署运行你感兴趣的模型镜像

libreoffice python SDK使用教程

环境

centos6.9,libreoffice4.3.7.2

安装libreoffice4.3.7.2:

从https://downloadarchive.documentfoundation.org/libreoffice/old/下载libreoffice4.3.7.2及其sdk.

启动脚本

可根据以下示例写一个shell脚本启动libreoffice服务进程。

#!/bin/bash
/home/xxx/libreoffice4.3.7.2/opt/libreoffice4.3/program/soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"

编写python code

如果libreoffice 安装到本地home下,那么启动终端运行python脚本时需要先设置PYTHONPATH.

setenv PYTHONPATH <install_path>/opt/libreoffice4.3/program  # cshexport PYTHONPATH=<install_path>/opt/libreoffice4.3/program  # bash
import uno
from com.sun.star.awt import Size
from com.sun.star.awt import Point
from com.sun.star.beans import PropertyValue

# 获取本地设备上下文
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
# 获取服务进程设备上下文
remote_context = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
desktop = remote_context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context)

model = desktop.getCurrentComponent()
sheet = model.CurrentController.ActiveSheet

# 向单元格写入值
cell = sheet.getCellRangeByName("C2")
cell.String = "Hello World"
cell2 = sheet.getCellRangeByName("C9")

# 嵌入svg矢量图对象(非链接的方式永久写入ods中)
gra_provider= remote_context.ServiceManager.createInstanceWithContext('com.sun.star.graphic.GraphicProvider', localContext)

graph = PropertyValue()
graph.Name = "URL"
graph.Value = uno.systemPathToFileUrl('/home/xxx/domo/test.svg')

image_obj = gra_provider.queryGraphic((graph, )) # 传入python元组,等价于libreoffice Sequence类型

image = model.createInstance('com.sun.star.drawing.GraphicObjectShape')
image.Graphic = image_obj

osize = image.Size
osize.Height = 6000
osize.Width = 6000
image.Size = osize

opos = image.Position
opos.X = 2000
opos.Y = 3000
image.Position = opos  # 这个值可以通过cellRange.getCellByPosition(nCol,nRow)获取到单元格的位置,然后将图片位置和单元格位置对应

draw_page = sheet.DrawPage
draw_page.add(image)

开发工具

MRI

为libreoffice 安装MRI插件,用来查看uno对象属性和方法。

插件地址:https://github.com/hanya/MRI

libreoffice4.3.7.2只能安装版本1.几的。

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

pyoo

PyOO allows you to control a running OpenOffice or LibreOffice program for reading and writing spreadsheet documents.

https://github.com/seznam/pyoo

pyoo对uno的一些接口进行了二次封装,可参考。

unoconv

https://github.com/unoconv/unoconv

libreoffice-python-library

https://github.com/CosminEugenDinu/libreoffice-python-library

libreoffice-python-library对libreoffice进程启动和停止进行了封装。

Learn more

https://www.jb51.net/article/164633.htm

https://blog.youkuaiyun.com/HYNzhl/article/details/69266236

https://blog.oio.de/2010/05/14/embed-an-image-into-an-openoffice-org-writer-document/

https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=80302

https://api.libreoffice.org/

fice-org-writer-document/

https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=80302

https://api.libreoffice.org/

https://api.libreoffice.org/docs/idl/ref/index.html
https://ask.libreoffice.org/t/python-script-blocks-mouse-events/63256/2

您可能感兴趣的与本文相关的镜像

Python3.11

Python3.11

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值