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.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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: LibreOffice Python是一种基于Python编程语言的LibreOffice扩展,它可以让用户通过编写Python脚本来扩展和自定义LibreOffice的功能。使用LibreOffice Python,用户可以创建自己的宏、插件和脚本,以满足自己的特定需求。此外,LibreOffice Python还提供了一些API和工具,使用户可以更轻松地访问和操作LibreOffice的各种功能和数据。 ### 回答2: LibreOffice Python是一个基于Python编程语言的LibreOffice的集成开发环境(IDE)。它允许开发人员使用Python编写宏、扩展和插件来扩展LibreOffice的功能和自定义LibreOffice的用户界面。 LibreOffice是一个免费的开源办公软件套件,其包括一个文本编辑器、电子表格、演示文稿、绘图和数据库管理软件等应用程序。 使用Python编程语言可以使开发人员更易于开发和维护LibreOffice的扩展和插件。Python是一种高级编程语言,它简单易学,并提供了广泛的库和模块来支持办公应用程序开发。此外,Python是一种跨平台编程语言,可以在不同的操作系统上运行。这意味着开发人员只需要编写一次代码,并可以在多个平台上使用。 LibreOffice Python还为开发人员提供了一个完整的开发环境,包括调试器、代码编辑器和测试工具。这些工具可以大大提高开发人员的生产力和代码的质量。无论是初学者还是有经验的开发人员都可以使用LibreOffice Python来扩展和自定义LibreOffice以满足其特定需求。 总的来说,LibreOffice Python是一个非常强大和有用的工具,它可以帮助开发人员轻松地扩展和自定义LibreOffice,提高办公应用程序的功能和自定义程度。 ### 回答3: LibreOffice Python是一个 Python脚本宏扩展,允许用户通过编写简单的Python代码对LibreOffice进行扩展和自定义。它集成了Python解析器,并提供了一个API,允许用户使用Python脚本来操作LibreOffice组件和文档。 LibreOffice Python是一种在LibreOffice中扩展和自定义功能的非常方便的方法。用户可以使用Python轻松地编写脚本来实现对文档的自动化处理,包括批量文件转换、自动生成报告和创建自定义工具栏等操作。此外,用户可以使用Python与其它库或Web服务进行集成,从而扩展其在LibreOffice中的功能。 使用LibreOffice Python非常容易,用户只需要在LibreOffice中启用Python宏扩展,然后编写Python脚本。用户可以创建Python脚本来操作文档对象、查找和替换文本、生成图形等等。 总之,LibreOffice Python为用户提供了一种快速和简单地扩展和自定义LibreOffice的方法。它提供了一个简单易用的API,让用户能够使用Python脚本轻松地实现各种操作,使LibreOffice称为满足用户需求的更加灵活、强大的工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值