python通过UNO访问LibreOffice/OpenOffice可以使用的命令列表、Filter列表

3 篇文章 0 订阅
1 篇文章 0 订阅

在python中通过UNO访问LibreOffice/OpenOffice,经常需要用到命令、Filter。因为LibreOffice/OpenOffice在线分档非常庞大,我找了好长时间才找到,记下来,方便后续查阅。
例如,下面的代码片段,用到了命令UpdateAllIndexes、SaveAs、ExportDirectToPDF:

dispatcher.executeDispatch(doc, ".uno:UpdateAllIndexes", "", 0, ())
dispatcher.executeDispatch(doc, ".uno:SaveAs", "", 0, tuple(opts))
dispatcher.executeDispatch(doc, ".uno:ExportDirectToPDF", "", 0, tuple([struct, struct2]))

要知道有些命令可以使用,请查阅命令列表:https://wiki.openoffice.org/wiki/Framework/Article/OpenOffice.org_3.x_Commands

例如,下面的代码片段用到了Filter:“MS Word 2007 XML”、“writer_pdf_Export”

   struct = uno.createUnoStruct("com.sun.star.beans.PropertyValue")
            struct.Name = "FilterName"
            struct.Value = "MS Word 2007 XML"
   struct2 = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
        struct2.Name = "FilterName"
        struct2.Value = "writer_pdf_Export"

要知道有哪些Filter可以使用,请查阅Filter列表:https://wiki.openoffice.org/wiki/Framework/Article/Filter/FilterList_OOo_3_0
https://help.libreoffice.org/latest/en-US/text/shared/guide/convertfilters.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LibreOffice可以通过Python UNO API与Python进行交互,可以使用Python编写脚本来控制LibreOffice的各种功能,包括创建、读取和修改Excel文件。 下面是一个简单的Python脚本,可以使用LibreOfficePython UNO API创建一个新的Excel文档,并向其中添加一些数据: ```python import uno import os # 启动LibreOffice localContext = uno.getComponentContext() resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext) context = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext") desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context) # 创建一个新的Excel文档 calc = desktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, ()) # 获取文档的第一个表格 sheets = calc.getSheets() sheet = sheets.getByIndex(0) # 向表格中添加一些数据 cell = sheet.getCellByPosition(0, 0) cell.setValue(1) cell = sheet.getCellByPosition(1, 0) cell.setValue(2) cell = sheet.getCellByPosition(2, 0) cell.setFormula("=sum(A1:B1)") # 保存文档 filename = os.path.join(os.getcwd(), "example.xlsx") calc.storeToURL("file://" + filename.replace("\\", "/"), ()) # 关闭文档 calc.close(True) ``` 这个脚本的作用是创建一个新的Excel文档,向其中添加一些数据,然后将其保存到指定的文件中。你可以根据自己的需求修改这个脚本,添加更多的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值