[Solved] Python - save individual scalc sheets

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
vesuvian
Posts: 9
Joined: Tue Feb 14, 2012 4:32 pm

[Solved] Python - save individual scalc sheets

Post by vesuvian »

Hi all

I am trying to do the following:

Code: Select all

	# Get the current document
	context = uno.getComponentContext()
	desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
	document = desktop.getCurrentComponent()
	
	# Get the sheet
	sheet = document.getSheets().getByName("CG_CHR")
	
	# Args for saving
	args = toProperties({"ReadOnly":False})
	# Save
	sheet.storeToURL("file://"+tmpSpreadsheet, args)
This fails because sheet has no storeToUrl method.

What is the correct way to do this?

Thanks!
Last edited by Hagar Delest on Thu Apr 26, 2012 4:32 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.1.1 - Centos4
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Python - save individual scalc sheets

Post by Charlie Young »

The best way to do it will depend on a number of factors, e.g.:

Does the sheet contain formulas referring to other sheets? If so, do you just want the values, or do you want to create links to the original document?

Do you want to preserve formatting?

If the sheet only contains formulas referring to itself, you could copy the sheet to a new document, then save the new document.

I would be happy to delve into this more, but I think we need more details.
Apache OpenOffice 4.1.1
Windows XP
vesuvian
Posts: 9
Joined: Tue Feb 14, 2012 4:32 pm

Re: Python - save individual scalc sheets

Post by vesuvian »

Fair points!

1) There are no formulas referring to other sheets

2) I need to preserve formatting on the first sheet (which I will save as an ODS) though the second sheet will be saved as a CSV

3) How can I copy a sheet to a new document? Will this be transparent to the user?

Thanks for your help!
OpenOffice 3.1.1 - Centos4
vesuvian
Posts: 9
Joined: Tue Feb 14, 2012 4:32 pm

Re: Python - save individual scalc sheets

Post by vesuvian »

It is also worth mentioning now: on the other side I need to write a macro that will load these individual files as new sheets in the current session. is this possible?
OpenOffice 3.1.1 - Centos4
User avatar
Villeroy
Volunteer
Posts: 31355
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Python - save individual scalc sheets

Post by Villeroy »

http://www.oooforum.org/forum/viewtopic.phtml?t=70072 links a new document's sheet to some sheet in the source file and then breaks the link. The result is a copy of the source sheet.
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
vesuvian
Posts: 9
Joined: Tue Feb 14, 2012 4:32 pm

Re: Python - save individual scalc sheets

Post by vesuvian »

For the benefit of anyone who also wants to do this, my solution is a little something like this:

Code: Select all

def saveSheetToFile(desktop, inFile, outFile, sheetName):
	
	# Make a new document
	document = desktop.loadComponentFromURL("private:factory/scalc", "_default", 0, ())
	# Add our new sheet
	sheets = document.getSheets()
	sheets.insertNewByName(sheetName, 0)
	sheet = document.getSheets().getByName(sheetName)
	sheet.link("file://" + inFile, sheetName, "calc8", "", NORMAL)
	# Break the link between the documents
	sheet.setLinkMode(NONE)
	# Tidy up
	removeDefaultSheets(document)
	
	# Save
	extension = outFile.split(".")[-1:][0]
	if extension == "csv":
		args = toProperties({"ReadOnly":False, "FilterName":"Text - txt - csv (StarCalc)", "FilterOptions":"59,34,76,1"})
	else:
		args = toProperties({"ReadOnly":False})
	
	document.storeToURL("file://"+outFile, args)
	
	# Close
	document.dispose()

Right now I am trying to figure out how to load in separate documents as new sheets.

Thanks!
OpenOffice 3.1.1 - Centos4
vesuvian
Posts: 9
Joined: Tue Feb 14, 2012 4:32 pm

Re: Python - save individual scalc sheets

Post by vesuvian »

Aha, it was as simple as linking the document back into a new sheet in the current document.

Thanks everyone!
OpenOffice 3.1.1 - Centos4
Post Reply