前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 操作 libreoffic

python 操作 libreoffic

作者头像
py3study
发布2020-01-13 15:49:47
1.8K0
发布2020-01-13 15:49:47
举报
文章被收录于专栏:python3python3

最近研究了一个,用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.

连接:

代码语言:javascript
复制
import uno

def connect():
    local = uno.getComponentContext()
    resolver = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local)
    context = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
    desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
 
    return desktop
class Calc:

    def __init__(self, file_name = "private:factory/swriter"):
        desktop = connect()
        self.doc = desktop.loadComponentFromURL(file_name, "_blank", 0, ())
        self.sheet = self.doc.getSheets().getByIndex(0)

    def get_dic(self, start, end):
 
        for row in range(start - 1, end):
            value = self.sheet.getCellByPosition(0, row).getString()
            self.sheet.getCellByPosition(1, row).setString("have read")
    def save(self):
        self.doc.store()
def main():
    calc_file = "file:///home/zhangliyong/Desktop/2011.xls"

    calc_obj = Calc(calc_file)
    calc_obj.get_dic(5, 46)
    calc_obj.save()

注意在操作libreoffice文档时,当指写文档路径时前面要加file://

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档