openoffice:在writer中复制表的行

Ale*_*lla 1 python templates openoffice.org openoffice-writer odt

我需要以编程方式在openoffice writer中复制Table的行.

这不难通过添加行table.Rows.insertByIndex(idx, count),那加空行,很容易到该行指定的文本添加DataArrayCellRange.通过这种方式,您可以放松对单元格样式的控制,特别是如果单元格具有不同样式(粗体/斜体)的单词,则会将其展平为同一个面.我需要的是以保留单元格/行中每个单词的样式的方式复制行.

这是使用openoffice的Python模板系统的最后一步(http://oootemplate.argolinux.org).我通过Python中的uno接口访问文档,但任何语言都可以解释它背后的逻辑.

Ale*_*lla 5

解决方案是使用控制器的方法.getTrasferable()从ViewCursor获取数据.这反过来要求您控制您的视图光标并将其放置在每个单元格中(我无法使ViewCursor跨越多个单元格).获得transferable后,将光标放在目标位置并插入.

  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)
Run Code Online (Sandbox Code Playgroud)