python - 如何在 OpenOffice.Org Calc 的单元格公式中调用 Python 宏?

标签 python openoffice-calc

为了扩展 OpenOffice 的功能,我在 user script directory 的文件中定义了一些 Python 宏。 (~/Library/Application Support/OpenOffice.org/3/user/Scripts/python/,在我的例子中)。宏在 Python 宏管理器中可见。但是,在单元格公式中使用这些函数会导致“#NAME?” (OO.org error 525)。

假设我定义了以下函数:

def pytype(val):
    return str(type(val))

如何在单元格公式中调用 pytype(例如 =PYTYPE("string"))?

背景

我正在将一些数据从 Authorize.net 导入 MySQL 数据库进行分析。 MySQL 无法将 Authorize.net 使用的日期和时间格式解析为 DATETIMETIMESTAMP 字段,因此我试图将数据转换为 MySQL 可以处理的格式进口前处理。 OpenOffice 也无法将数据识别为日期和时间,据我所知,OO.Org 没有通用的日期解析功能。因此,我正在扩展 OO.org 的功能。

还有其他方法可以解决更大的问题。例如,我还可以尝试使用额外的列来修复 MySQL 导入后的数据。事实上,这是我第一次做的;但是,现在表中已有数据需要处理。正因为如此,也因为 future 还有其他任务我希望通过在公式中使用宏来完成,目前我最感兴趣的是在公式中调用 Python 宏。

最佳答案

在旧的 OO.org 论坛上,( super )用户 Villeroy 发布了如何调用 Python functions from OO.org Basic 的插图。 ,然后可以在公式中使用。关键是使用 com.sun.star.script.provider.MasterScriptProviderFactory 服务作为桥梁。这是他的解决方案的改编版,概括为在任意模块中调用任意函数:

REM Keep a global reference to the ScriptProvider, since this stuff may be called many times: 
Global g_MasterScriptProvider as Object
REM Specify location of Python script, providing cell functions: 
Const URL_Main as String = "vnd.sun.star.script:" 
Const URL_Args as String = "?language=Python&location=user" 

Function invokePyFunc(file AS String, func As String, args As Array, outIdxs As Array, outArgs As Array)
   sURL = URL_Main & file & ".py$" & func & URL_Args
   oMSP = getMasterScriptProvider()
   On Local Error GoTo ErrorHandler
      oScript = oMSP.getScript(sURL)
      invokePyFunc = oScript.invoke(args, outIdxs, outArgs)
      Exit Function
   ErrorHandler:
      Dim msg As String, toFix As String
      msg = Error$
      toFix = ""
      If 1 = Err AND InStr(Error$, "an error occurred during file opening") Then
         msg = "Couldn' open the script file."
         toFix = "Make sure the 'python' folder exists in the user's Scripts folder, and that the former contains " & file & ".py."
      End If
      MsgBox msg & chr(13) & toFix, 16, "Error " & Err & " calling " & func
end Function

Function getMasterScriptProvider() 
   if isNull(g_MasterScriptProvider) then 
      oMasterScriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory") 
      g_MasterScriptProvider = oMasterScriptProviderFactory.createScriptProvider("") 
   endif 
   getMasterScriptProvider = g_MasterScriptProvider
End Function

然后可以使用它来创建可在公式中调用的 OO.org Basic 函数。使用示例 pytype:

Const libfile as String = "util"    REM functions live in util.py

Function pytype(value)
    pytype = invokePyFunc(libfile, "pytype", Array(value), Array(), Array())
End Function

另一个可能的实现是创建一个 Python add-in .但是,这是一个更繁重的选择,因为它需要安装 OpenOffice SDK,而且我不清楚这种方法是适用于免费功能还是仅适用于类。

关于python - 如何在 OpenOffice.Org Calc 的单元格公式中调用 Python 宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7591656/

相关文章:

python - 为什么 pyinstaller 创建的 .exe 文件不起作用?

openoffice.org - 自由办公室 : Filtering with a list of value

Python 和引用传递。局限性?

python - 使用 BeautifulSoup 问题抓取数据

OpenOffice.org Calc SUMIF 语句

matlab - 将 .ods 工作表加载到 Octave 时出错

openoffice.org - 向宏中的单元格添加注释

PHPExcel OpenOffice Calc (ods) 文件问题

python - 在 KV 文件中为 GridView 插入一条线和颜色边框

python - 创建表后向 SQLAlchemy 模型添加索引