如何使用LibreOffice SDK与Python在Calc中创建自定义侧边面板
在LibreOffice Calc中用Python+LibreOffice SDK创建自定义侧边面板
一、环境准备
- 安装LibreOffice及对应版本的SDK,使用LibreOffice自带的Python环境(通常位于安装目录的
program/python.exe),避免版本兼容问题。 - 确保Python能调用UNO组件:可直接用LibreOffice自带Python运行脚本,无需额外配置
PYTHONPATH。
二、定义侧边面板UI配置
创建CustomPanel.xcu配置文件,用于注册侧边面板的基本信息:
<?xml version="1.0" encoding="UTF-8"?> <oor:component-data xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="UI" oor:package="org.openoffice.Office"> <node oor:name="Sidebar"> <node oor:name="Panels"> <node oor:name="com.example.CustomCalcPanel" oor:op="replace"> <prop oor:name="Title"><value>我的自定义面板</value></prop> <prop oor:name="PanelFactoryURL"><value>vnd.sun.star.script:CustomPanelScript.createPanel?language=Python&location=user</value></prop> <prop oor:name="ContextList"><value>com.sun.star.sheet.SpreadsheetDocument</value></prop> </node> </node> <node oor:name="DeckOrder"> <node oor:name="com.sun.star.sheet.SpreadsheetDocument"> <node oor:name="Panels"> <node oor:name="com.example.CustomCalcPanel" oor:op="replace"> <prop oor:name="Position"><value>1</value></prop> </node> </node> </node> </node> </node> </oor:component-data>
Title:侧边面板显示名称PanelFactoryURL:指定创建面板的Python脚本入口ContextList:限制面板仅在Calc文档中显示
三、编写Python面板逻辑
创建CustomPanelScript.py脚本,实现面板控件及交互逻辑:
import uno from com.sun.star.awt import Rectangle from com.sun.star.ui import XSidebarPanel class CustomPanelImpl(XSidebarPanel): def __init__(self, ctx): self.ctx = ctx def createPanel(self, parent): # 创建容器控件 container = self.ctx.ServiceManager.createInstanceWithContext( "com.sun.star.awt.UnoControlContainer", self.ctx ) container.setPeer(parent) # 添加测试按钮 btn = self.ctx.ServiceManager.createInstanceWithContext( "com.sun.star.awt.UnoControlButton", self.ctx ) btn_model = btn.getModel() btn_model.Name = "TestButton" btn_model.Label = "点击测试" btn_model.Position = Rectangle(20, 20, 120, 30) btn.addActionListener(self) container.addControl("TestButton", btn) return container def actionPerformed(self, event): # 按钮点击事件:弹出提示框 desktop = self.ctx.ServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop", self.ctx ) doc = desktop.getCurrentComponent() if doc.supportsService("com.sun.star.sheet.SpreadsheetDocument"): msg_box = self.ctx.ServiceManager.createInstanceWithContext( "com.sun.star.awt.MessageBox", self.ctx ) msg_box.setParent(doc.getCurrentController().getFrame().getContainerWindow()) msg_box.MessageType = 1 # 信息提示框 msg_box.Buttons = 1 # 仅OK按钮 msg_box.setMessage("自定义面板按钮已触发!") msg_box.execute() def createPanel(ctx, *args): return CustomPanelImpl(ctx)
- 实现
XSidebarPanel接口,通过createPanel方法构建面板控件 - 绑定按钮点击事件,调用UNO API实现交互逻辑
四、打包为LibreOffice扩展
- 创建如下目录结构:
CustomCalcPanel/ ├── META-INF/ │ └── manifest.xml ├── CustomPanelScript.py ├── CustomPanel.xcu └── description.xml
- 编写
manifest.xml:
<?xml version="1.0" encoding="UTF-8"?> <manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest"> <manifest:file-entry manifest:media-type="application/vnd.sun.star.configuration-data" manifest:full-path="CustomPanel.xcu"/> <manifest:file-entry manifest:media-type="application/vnd.sun.star.script" manifest:full-path="CustomPanelScript.py"/> <manifest:file-entry manifest:media-type="application/vnd.sun.star.package-bundle-description" manifest:full-path="description.xml"/> </manifest:manifest>
- 编写
description.xml(可选,用于描述扩展):
<?xml version="1.0" encoding="UTF-8"?> <description xmlns="http://openoffice.org/extensions/description/2006"> <version value="1.0"/> <identifier value="com.example.CustomCalcPanel"/> <display-name> <name lang="zh-CN">Calc自定义侧边面板</name> </display-name> </description>
- 将整个目录打包为ZIP文件,重命名为
CustomCalcPanel.oxt
五、安装与测试
- 打开LibreOffice,点击「工具」→「扩展管理器」,添加
CustomCalcPanel.oxt,重启LibreOffice - 打开Calc文档,右侧侧边栏会显示「我的自定义面板」,点击按钮可触发提示框
内容的提问来源于stack exchange,提问作者Anton
相关产品推荐
相关产品推荐

