You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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&amp;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扩展

  1. 创建如下目录结构:
CustomCalcPanel/
├── META-INF/
│   └── manifest.xml
├── CustomPanelScript.py
├── CustomPanel.xcu
└── description.xml
  1. 编写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>
  1. 编写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>
  1. 将整个目录打包为ZIP文件,重命名为CustomCalcPanel.oxt

五、安装与测试

  • 打开LibreOffice,点击「工具」→「扩展管理器」,添加CustomCalcPanel.oxt,重启LibreOffice
  • 打开Calc文档,右侧侧边栏会显示「我的自定义面板」,点击按钮可触发提示框

内容的提问来源于stack exchange,提问作者Anton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 13:51:09