简体   繁体   English

如何将 python 脚本嵌入 LibreOffice Calc

[英]How to embed python scripts into LibreOffice Calc

I have a successful.py script that can open an existing LO Calc doc (saved as.xlsx so that I can use openpyxl) which appends information into the file which saves the file and closes it on termination of the script.我有一个成功的.py 脚本,它可以打开现有的 LO Calc 文档(保存为 .xlsx,以便我可以使用 openpyxl),它将信息附加到文件中,该文件保存文件并在脚本终止时关闭它。 The.xlsx file cannot already be open when the script runs, or the file gets corrupted and all data is lost.脚本运行时,.xlsx 文件无法打开,或者文件损坏并且所有数据都丢失。

But what I really need is to be able to see the data populating the spreadsheet as it happens so I can have a dynamic chart visually displaying the information.但我真正需要的是能够看到填充电子表格的数据,这样我就可以有一个动态图表直观地显示信息。

I installed APSO (tq.) which I believed would let me to embed editable python user scripts.我安装了 APSO (tq.),我相信它可以让我嵌入可编辑的 python 用户脚本。 I want the script to when I click on a programmable button I made in my sheet.当我单击我在工作表中制作的可编程按钮时,我希望脚本能够执行。

I saved the script at /home/USER/.config/libreoffice/4/user/Scripts/python which I made in accordance to instructions from the ScriptForge 'code assistant' software from the LO Conference 2021.我将脚本保存在 /home/USER/.config/libreoffice/4/user/Scripts/python 中,该脚本是根据 LO Conference 2021 中 ScriptForge“代码助手”软件的说明制作的。

In the macro selector window, under "My Macros", I see my script, but access to the "OK" button is denied to me (grayed out), yet I am able to select a script from the "LibOCon_2021.py" ScriptForge file.在宏选择器 window 中,在“我的宏”下,我看到了我的脚本,但我拒绝访问“确定”按钮(灰显),但我能够 select 来自“LibOCon_2021.py”ScriptForge 的脚本文件。

Comparing my.py script to the ones in "LibOCon_2021.py", It seemed the difference was that my script didn't have a definition, so at the top of my script I added "def to_be_embedded(args=None):" and saved it, but the "OK" button is still not available so I cannot tie it to the button press.将 my.py 脚本与“LibOCon_2021.py”中的脚本进行比较,似乎不同之处在于我的脚本没有定义,所以在我的脚本顶部添加了“def to_be_embedded(args=None):”和保存了它,但“确定”按钮仍然不可用,所以我无法将它与按钮绑定。

So how do I actually embed my script and get it to run when I click on the button?那么,当我单击按钮时,如何实际嵌入我的脚本并让它运行呢?

Here are steps to create a button that runs an embedded python macro.以下是创建运行嵌入式 python 宏的按钮的步骤。

  1. Create a new Calc spreadsheet.创建一个新的 Calc 电子表格。 It doesn't even need to be saved for this example to work, but if you do save then use.ods format.这个例子甚至不需要保存它就可以工作,但如果你保存然后使用.ods 格式。
  2. With APSO installed, go to Tools -> Macros -> Organize python scripts .安装 APSO 后,go 到Tools -> Macros -> Organize python scripts
  3. Select "Untitled 1" to embed the macro there, and in the Menu, select Create module and OK. Select "Untitled 1" 将宏嵌入其中,然后在菜单中,select创建模块并确定。 (This is embedded in the spreadsheet file, unlike in your question where the script was placed in the LibreOffice user directory, which is what "My Macros" means). (这嵌入在电子表格文件中,与您将脚本放置在 LibreOffice 用户目录中的问题不同,这就是“我的宏”的含义)。
  4. Select the newly created file called "Module" and the menu will now contain different options. Select 新创建的文件名为“模块”,菜单现在将包含不同的选项。 Select Edit . Select编辑 If this doesn't work, configure APSO to use the correct text editor.如果这不起作用,请将 APSO 配置为使用正确的文本编辑器。
  5. Enter the following code and save (for example :w if using vim).输入以下代码并保存(例如:w如果使用 vim)。
def test(args=None):
    raise Exception("Hello from Python!")
  1. From the Form Controls toolbar, create a Push Button.从表单控件工具栏中,创建一个按钮。 Calc must be in Design Mode to do this. Calc 必须处于设计模式才能执行此操作。
  2. Right-click on the push button and choose Control Properties .右键单击按钮并选择Control Properties Under Events , select the dots next to Execute action and then press Macro .事件下,select执行操作旁边的点,然后按 Expand "Untitled 1" and select "Module", where "test" will show up as the macro name.展开“Untitled 1”和 select“Module”,其中“test”将显示为宏名称。 Press OK.按确定。

Now turn off Design mode and click on the button.现在关闭设计模式并单击按钮。 We can see that it runs because it produces the following message:我们可以看到它运行,因为它产生以下消息:

com.sun.star.uno.RuntimeException: Error during invoking function test in
module vnd.sun.star.tdoc:/1/Scripts/python/Module.py (<class 'Exception'>:
Hello from Python!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM