OpenOffice:在Writer中复制表格行
我需要在OpenOffice Writer中通过编程的方式复制表格的行。
其实通过 table.Rows.insertByIndex(idx, count)
来添加行并不难,这个方法会添加空行,之后在这些行中填入文本也很简单,只需要把 DataArray
赋值给 CellRange
就可以了。不过,这样做会失去对单元格样式的控制,特别是如果一个单元格里的文字有不同的样式(比如粗体或斜体),它们会被统一成一种样式。我需要的是以一种方式复制行,能够保留每个单元格/行中每个单词的样式。
这是一个使用OpenOffice的Python模板系统的最后一步(http://oootemplate.argolinux.org)。我通过Python中的uno接口访问文档,但用任何语言来解释这个逻辑都是可以的。
1 个回答
5
解决办法是使用控制器的方法 .getTrasferable() 来从视图光标中获取数据。这就意味着你需要控制你的视图光标,并将它放在每一个单元格里(我没办法让视图光标跨越多个单元格)。一旦你获取了可转移的数据,就把光标放到目标位置,然后进行插入。
desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
document = desktop.loadComponentFromURL("file://%s/template-debug.odt" % os.getcwd() ,"_blank", 0, ())
controller=document.getCurrentController()
table = document.TextTables.getByIndex(0)
view_cursor=controller.getViewCursor()
src = table.getCellByName(src_name)
dst = table.getCellByName(dst_name)
view_cursor.gotoRange(src.Text, False)
txt = controller.getTransferable()
view_cursor.gotoRange(dst.Text, False)
controller.insertTransferable(txt)