1

pyuno を使用して LibreOffice calc ドキュメントを開き、セルの範囲を定義して並べ替えたいと思います。コードは次のとおりです。

import os
import uno

# open a calc document
# (it is assumed that LibreOffice is launched with the command line:
# soffice -accept="socket,host=localhost,port=2002;urp")
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)
url = uno.systemPathToFileUrl(os.path.abspath("c:/aaa/essai.xls"))
doc = desktop.loadComponentFromURL(url,"_blank", 0, ())

# range to be sorted
range = doc.Sheets.getByIndex(0).getCellRangeByPosition(0,0,4,4)
# use the first column for the sort key 
colDescr = uno.createUnoStruct(
  'com.sun.star.table.TableSortField')
colDescr.Field = 0 
# sort descriptor
sortDescr = range.createSortDescriptor()
for x in sortDescr: 
  if x.Name == 'SortFields':
    x.Value = (colDescr,)
    break
else:
  raise KeyError('SortFields')
# sort ...
range.sort(sortDescr)

このコードは正しく解釈されますが、何もしません (計算ドキュメントで行がソートされません) 間違っていますか? sortDescr には正しいタイプ (PropertyValue タプル) があると思いますが、よくわかりません。Windows 7、LibreOffice 4.3.4.1、Python 3.3 を使用しています。回答ありがとうございます。

4

1 に答える 1