排序单元格范围与pyuno一个计算文档中(Sorting cell range in a calc

2019-10-21 08:05发布

随着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)

此代码是正确的解释,但不执行任何操作(该行不计算文档中排序)Am'I错了吗? 我认为sortDescr有正确的类型(元组的PropertyValue),但我不知道。 我使用的是Windows 7,LibreOffice的4.3.4.1,Python的3.3感谢您的答案。

Answer 1:

更改该行:x.Value =(colDescr)

由:x.Value = uno.Any( '[] com.sun.star.table.TableSortField',(aCriterios,))



文章来源: Sorting cell range in a calc document with pyuno