本文主要是介绍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 # csh
或
export 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_objosize = image.Size
osize.Height = 6000
osize.Width = 6000
image.Size = osizeopos = 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.csdn.net/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
这篇关于libreoffice python SDK使用教程(包括插入svg矢量图)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!