python 操作 libreoffice spreadsheet

最近研究了一个,用python来向libreoffice spreadsheet中的写数据,openoffice管方网站的资料也不是很清楚,

在网上找到的几个比较有用的网站有:http://wiki.services.openoffice.org/wiki/Python#PyUNO_Modules

上面了有一个比较好的入门文档:http://lucasmanual.com/mywiki/OpenOffice

还有一个人在论坛上写的一个比较有用的例子:http://www.oooforum.org/forum/viewtopic.phtml?p=56037#56037

这个例子非常好,告诉你怎样操作spreadsheet,本人在其它地方没有找到操作spreadsheet的文档,如果哪位网友找到了,留个言,谢谢!

现在把基本的代码贴过来,其它的请查上面给出的那两个网址即可


首先要启动libreoffice用下面命令:
libreoffice -accept="socket,host=localhost,port=2002;urp;"

Start openoffice so it listens on a port.

连接:

import uno

def connect():
    local = uno.getComponentContext()
    resolver = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local)
    context = resolver.resolve("uno:socket,hos

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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文档,向其中添加一些数据,然后将其保存到指定的文件中。你可以根据自己的需求修改这个脚本,添加更多的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值