[Calc] Python macro: Save as

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
antrrax
Posts: 6
Joined: Wed Feb 13, 2019 10:50 pm

[Calc] Python macro: Save as

Post by antrrax »

I have a button in a .ODS spreadsheet with a python macro that modifies some cells.
I need to save this modified .ODS file in another directory and under another name via python.

how to do this?
OpenOffice 4.1.6
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: python macro - save as ? Calc

Post by FJCC »

Here is a minimal example of doing a Save As in Python.

Code: Select all

def SaveAs():
  oDoc = XSCRIPTCONTEXT.getDocument()
  NewURL = "file:///home/fjcc/NewName.ods"
  oDoc.storeToURL(NewURL,())
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.
Ratslinger
Posts: 34
Joined: Sun Mar 01, 2015 3:34 am

Re: python macro - save as ? Calc

Post by Ratslinger »

Please note this was cross posted and also answered here:

https://ask.libreoffice.org/en/question ... e-as-calc/
LibreOffice 7.0.2.2
OpenOffice 4.0.1
Ubuntu Mate 20.04.1
Mint 20.3
antrrax
Posts: 6
Joined: Wed Feb 13, 2019 10:50 pm

Re: python macro - save as ? Calc

Post by antrrax »

FJCC wrote:Here is a minimal example of doing a Save As in Python.

And to save the current file, how do I do?
I tried like this:

Code: Select all

def SaveALL():
    oDoc = XSCRIPTCONTEXT.getDocument()
    originalFile= oDoc.URL
    oDoc.storeToURL(originalFile,())

    NewURL = "file:///home/fjcc/NewName.ods"
    oDoc.storeToURL(NewURL,())
If I change the document, the save icon changes from blue to red.
If I apply this macro above, it saves the current file, but libreoffice calc doesn't understand that the file has been saved and the icon is still red and when closing the document manually displays a dialog asking to save or discard changes.

What is the correct way to save the current file in python?
OpenOffice 4.1.6
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: python macro - save as ? Calc

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