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

2024-01-02 23:48

本文主要是介绍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_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矢量图)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/563991

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos