[python] how to copy cell and data with format?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
strongleg
Posts: 10
Joined: Sat Oct 28, 2017 4:41 pm

[python] how to copy cell and data with format?

Post by strongleg »

I'm trying to copy some cells (or may be rows or the whole sheet is better) with its format to other sheet. I have tried the way in the following post:
viewtopic.php?f=45&t=89172&p=420130&hil ... at#p420130
eg: I use the Cursor to get data and paste it to other sheet.
But with this way, data I got is pure data without format information(font, cell border, etc). so the dest cell has no format.

So how can i copy and paste cells(or row or sheet) with format information? thanks!
OpenOffice 2.4 on Ubuntu 9.04
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [python] how to copy cell and data with format?

Post by FJCC »

If you are going to write macros, you need to get the MRI extension so you can look at the properties and methods of your objects.

You can use the copyRange() method of a sheet to copy cell and keep the formatting.

Code: Select all

def CopyRange():
  doc = XSCRIPTCONTEXT.getDocument()
  sheets = doc.Sheets
  sheet1 = sheets.getByName('Sheet1')
  cursor = sheet1.createCursor()
  cursor.gotoStartOfUsedArea(False)
  cursor.gotoEndOfUsedArea(True)
  RngAddr = cursor.getRangeAddress()
  
  sheet2 = sheets.getByName('Sheet2')
  cell = sheet2.getCellRangeByName('A1')
  cellAddr = cell.CellAddress #use this as the upper left of the paste range
  
  sheet1.copyRange(cellAddr, RngAddr)
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [python] how to copy cell and data with format?

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply