[英]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 宏的按钮的步骤。
:w
if using vim).输入以下代码并保存(例如:w
如果使用 vim)。def test(args=None):
raise Exception("Hello from Python!")
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.