我有.iqy文件,它是Excel的Internet查询文件。如果我使用LibreOffice Calc打开该文件,则可以正确填充工作表中的数据,并可以根据需要使用GUI保存它。
我的问题是如何以无头模式打开该文件,以便将填充的文件另存为.xls文件?首选的解决方案是bash脚本编写或Python,因为我可以在当前项目中轻松实现它们。
我要在无头模式下执行的步骤如下:
我可以使用GUI轻松实现它们,但是在服务器上,我应该能够通过脚本在没有GUI的情况下完成这些操作。
编辑:似乎没有其他方法可以尝试通过LibreOffice API进行。如果设法找到解决方法,我会在此处发布更新。
答案 0 :(得分:0)
我设法使它与python和uno一起使用。下面是我的代码
import uno
from com.sun.star.beans import PropertyValue
def convert_iqy_excel(xls_path, socket="socket", host="localhost", port="2002"):
# get the uno component context from the PyUNO runtime
localContext = uno.getComponentContext()
# create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext )
# connect to the running office
ctx = resolver.resolve("uno:{},host={},port={};urp;StarOffice.ComponentContext".format(socket,host,port))
smgr = ctx.ServiceManager
# get the central desktop object
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
# access the current calc document
doc = desktop.getCurrentComponent()
# save as f_name
f_name = "file://{}".format(xls_path)
args = (PropertyValue('FilterName', 0, 'MS Excel 97', 0),)
doc.storeToURL(f_name, args)
# Do a nasty thing before exiting the python process. In case the
# last call is a oneway call (e.g. see idl-spec of insertString),
# it must be forced out of the remote-bridge caches before python
# exits the process. Otherwise, the oneway call may or may not reach
# the target object.
# I do this here by calling a cheap synchronous call (getPropertyValue).
ctx.ServiceManager
doc.dispose()
我有LibreOffice 6.0.7.3和Python 3.6.7