Page 1 of 1
[Solved] Python - save individual scalc sheets
Posted: Fri Apr 20, 2012 4:20 pm
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!
Re: Python - save individual scalc sheets
Posted: Fri Apr 20, 2012 6:24 pm
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.
Re: Python - save individual scalc sheets
Posted: Mon Apr 23, 2012 11:17 am
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!
Re: Python - save individual scalc sheets
Posted: Mon Apr 23, 2012 5:26 pm
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?
Re: Python - save individual scalc sheets
Posted: Mon Apr 23, 2012 5:37 pm
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.
Re: Python - save individual scalc sheets
Posted: Wed Apr 25, 2012 7:33 pm
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!
Re: Python - save individual scalc sheets
Posted: Thu Apr 26, 2012 3:46 pm
by vesuvian
Aha, it was as simple as linking the document back into a new sheet in the current document.
Thanks everyone!