Home

Tags

Запись в запущенный openoffice calc из python

2012-01-13 python openoffice libreoffice

Запускаем openoffice:

soffice "-accept=socket,host=localhost,port=2002;urp;"


Подключаемся и пишем:
# coding:utf8

import uno

localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
            "com.sun.star.bridge.UnoUrlResolver", localContext)

# Подключение к запущенному openoffice
ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)

# Получить текущй документ
model = desktop.getCurrentComponent()

# Установить заголовок
model.setTitle('Hello')

# Получить таблицу
sheets = model.getSheets()
sheet1 = getattr(sheets, sheets.ElementNames[0])

# Записать массив    
sheet1.getCellRangeByPosition(0,5,2,7).setDataArray( ((1, 'hello', 2), (3, 4, 5), ('a', 'b', 'c')) )

# Записать по ячейкам
for i in xrange(5):
    for j in xrange(5):
        cell = sheet1.getCellByPosition(i, j)
        #cell.setFormula(str(i * j))
        cell.setValue(i*j)


Проверено на Ubuntu 11.10 + Python 2.7 + LibreOffice 3.4.4