Ask Your Question

sup's profile - activity

2018-05-28 13:56:07 +0100 commented answer How to change print page size (page setup)

That is greyed out on my Ubuntu Box (Libreoffice 6.0). Any idea why?

2015-08-30 13:11:54 +0100 received badge  Notable Question (source)
2015-07-21 17:55:18 +0100 received badge  Notable Question (source)
2015-07-21 17:55:18 +0100 received badge  Famous Question (source)
2015-05-06 18:16:39 +0100 commented answer How can I put three languages to quick language change menu?

I opened a bug about this: https://bugs.documentfoundation.org/s...

2015-03-24 23:06:51 +0100 received badge  Notable Question (source)
2015-03-24 23:06:51 +0100 received badge  Famous Question (source)
2014-11-13 10:57:24 +0100 received badge  Famous Question (source)
2014-09-21 09:25:39 +0100 received badge  Popular Question (source)
2014-09-19 02:33:18 +0100 received badge  Enthusiast
2014-09-18 11:37:18 +0100 asked a question How do I link data from embedded spreadsheet in writer?

I have an embedded spreadsheet (it is editable directly in writer and does not exist as a separate file) in my writer document. I would like to reference data from that spreadsheet in my writer document so that when I update the embedded spreadsheet, the data in the writer will change accordingly. How would I go about it?

2014-08-30 21:04:49 +0100 received badge  Popular Question (source)
2014-08-29 12:11:42 +0100 marked best answer How can I put three languages to quick language change menu?

I deal with documents in three languages (Czech, French and English). In the bottom of the screen, I can see Czech, English (UK), English (USA). How can I make it to be Czech, English (UK), French (France)?

2014-08-29 12:11:29 +0100 commented answer How can I put three languages to quick language change menu?

Hm, but this means that when I deal with documents written by someone else, this options will not be there in the status bar, right? Can I force them to be there no matter what the original documents has?

2014-08-28 14:36:19 +0100 received badge  Famous Question (source)
2014-08-26 15:19:28 +0100 received badge  Notable Question (source)
2014-08-26 14:26:19 +0100 commented answer How do you close LibreOffice Calc from a macro?

Oh, I have not realized that actually meant something. It is easy to change:-).

2014-08-26 14:03:26 +0100 answered a question How do you close LibreOffice Calc from a macro?

In python, I do it like this. First, I run a bash script that launches a libreoffice instance that I can connect to:

#/bin/bash
# First we check if any other libreoffice instances are already running
# of course, if an instance is opened meanwhile, it will be closed.    
ps cax | grep soffice.bin > /dev/null
if [ $? -eq 0 ]; then # libreoffice is running, so just close the window we opened
    arg="--close-window"
 else # it was not running, kill it all
    arg="--close-office"
 fi
libreoffice "--accept=pipe,name=some_name;urp;StarOffice.Servicemanager" ~/test.ods &
sleep 3 # we need time to connect    
~/.config/libreoffice/4/user/Scripts/python/vytvor_fakturu.py $arg

Credit for checking if the process is running goes here. my_script.py is then (largely based on this) like this:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""In Libreoffice 4, python3 is default, so this script needs to be python3 too
Unicode is just good practise.
I guess this line is different on Windows."""

import uno
from pythonscript import ScriptContext
import sys

def connect_to_office():
     """We connect to libreoffice only when run externally.
     If that is the case, XSCRIPTCONTEXT is not in global scope."""
    if not 'XSCRIPTCONTEXT' in globals():
        localContext = uno.getComponentContext()
        resolver = localContext.ServiceManager.createInstanceWithContext(
                         'com.sun.star.bridge.UnoUrlResolver', localContext )
        client = resolver.resolve("uno:pipe,"
                        "name=some_name;" 
                        "urp;"
                        "StarOffice.ComponentContext")
        global XSCRIPTCONTEXT
        XSCRIPTCONTEXT = ScriptContext(client, None, None)

"""this is the macro that would be run firectly from libreoffice"""
def run_all():
    connect_to_office()
    oDoc = XSCRIPTCONTEXT.getDocument()
    """I assume a calc file here, this prints Hello world to cell A1"""
    oSheet = oDoc.CurrentController.ActiveSheet
    oCell = oSheet.getCellRangeByName("A1")
    oCell.String = 'Hello world!'
    if cmd_argument == '--close-office':
        oDoc.close(0) # if we do not close the document first, libreoffice will ask if we want to save changes
        d = XSCRIPTCONTEXT.getDesktop()
        d.terminate() # this closes all instances, I think
    elif cmd_argument == '--close-window':
        oDoc.close(0) # this just closes the document we connected to but soffice.bin will continue to run

    """Libreoffice allows to run functions as macros, but in a script
executed from a shell, any function needs to be run, but we must
make sure it is not run twice in case it is run directly
from libreoffice."""
if __name__ == "__main__":
     if len(sys.argv) > 1: # do not throw exceptions when the script is run without arguments
        run_all(sys.argv[1])
"""Make only this macro visible to libreoffice UI.
I.e., it hides connecto_to_office macro.
The comma at the end is important."""
g_exportedScripts = run_all,
2014-08-26 13:49:17 +0100 commented answer How do I run python macro from the command line?

Ah, good, thanksI updated the answer. How would ipython notebooks help? By reusing some of the code? I could do that with pythons files as well, if I needed to, or not? anyway, I am writing only this macro and the code must reside somewhere anyway. Or what am I missing?

2014-08-26 12:23:45 +0100 commented answer Executing macro on server without GUI

For python macros, see this .

2014-08-26 12:18:48 +0100 commented answer How do I run python macro from the command line?

Alright, I figured it out, but this seems to be insanely complex (see my answer bellow)!

2014-08-26 12:17:55 +0100 answered a question How do I run python macro from the command line?

It seems hackish, but based on this , the following works:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""In Libreoffice 4, python3 is default, so this script needs to be python3 too
Unicode is just good practise
I guess this line is different on Windows."""

import uno
from pythonscript import ScriptContext
import sys

def connect_to_office():
     """We connect to libreoffice only when run externally.
     If that is the case, XSCRIPTCONTEXT is not in global scope."""
    if not 'XSCRIPTCONTEXT' in globals():
        localContext = uno.getComponentContext()
        resolver = localContext.ServiceManager.createInstanceWithContext(
                         'com.sun.star.bridge.UnoUrlResolver', localContext )
        """It is assumed libreoffice is run like this from a shell:
        libreoffice "--accept=pipe,name=some_name;urp;StarOffice.Servicemanager ~/test.ods"
        """
        client = resolver.resolve("uno:pipe,"
                        "name=some_name;" 
                        "urp;"
                        "StarOffice.ComponentContext")
        global XSCRIPTCONTEXT
        XSCRIPTCONTEXT = ScriptContext(client, None, None)

"""this is the macro that would be run firectly from libreoffice"""
def run_all():
    connect_to_office()
    oDoc = XSCRIPTCONTEXT.getDocument()
    """I assume a calc file here, this prints Hello world to cell A1"""
    oSheet = oDoc.CurrentController.ActiveSheet
    oCell = oSheet.getCellRangeByName("A1")
    oCell.String = 'Hello world!'

    """Libreoffice allows to run functions as macros, but in a script
executed from a shell, any function needs to be run, but we must
make sure it is not run twice in case it is run directly
from libreoffice."""
if __name__ == "__main__":
    run_all()
"""Make only this macro visible to libreoffice UI.
I.e., it hides connecto_to_office macro.
The comma at the end is important."""
g_exportedScripts = run_all,

This scipt can be run both directly from inside Libreoffice and from the command line (in that case, libreoffice needs to be already running). Something like this works for me:

#!/bin/bash
libreoffice "--accept=pipe,name=some_name;urp;StarOffice.Servicemanager" ~/Desktop/test.ods &
sleep 3 # we need to wait till libreoffice starts, 3 seconds is more than enough for my computer but it YMMV
~/.config/libreoffice/4/user/Scripts/python/your_macro.py

Do not forget to make this file executable.

2014-08-26 12:01:03 +0100 received badge  Popular Question (source)
2014-08-26 10:19:20 +0100 received badge  Commentator
2014-08-26 10:19:20 +0100 commented answer How do I invoke IPython from a macro?

karolus: you award karma by upvoting answers and comments, I think.

2014-08-26 02:44:51 +0100 commented answer How can I export a PDF in a Python Macro?

Well, my knowledge of Windows is rather limited, so I was not sure. I guess replacing "/tmp/test.pdf" for "C:/test.pdf" or something should work, but I am not even sure what a proper path on windows is, I have not used it almost at all since over ten years:-).

2014-08-26 02:41:20 +0100 asked a question How do I run python macro from the command line?

This concerns Basic macros. How can I launch python macros from the command line? Libreoffice launches, does not run the macro and does not throw any errors, which is mostly unhelpful.

2014-08-26 00:55:03 +0100 answered a question How can I export a PDF in a Python Macro?

Well, it is actually pretty simple:

#!/usr/bin/python3
import uno
from com.sun.star.beans import PropertyValue
def make_pdf():
    properties=[] 
    p=PropertyValue() 
    p.Name='FilterName' 
    p.Value='calc_pdf_Export' 
    properties.append(p) 
    oDoc = XSCRIPTCONTEXT.getDocument()
    oDoc.storeToURL('file:///tmp/test.pdf',tuple(properties))

It will save a PDF version of your document into /tmp/test.pdf (on Linux, I am not sure what it would do on Windows).

This is based on this thread

2014-08-26 00:49:52 +0100 asked a question How can I export a PDF in a Python Macro?

I am writing a python macro and I need to export my file to PDF. How do I do it?

2014-08-25 21:32:33 +0100 commented answer How do I invoke IPython from a macro?

Well, I do not really see any advantage in webbased tool compared to a terminal based one - if I understand it correctly, it is good for presentation or mingling with graphs and so on, but neither is my use case, am I missing something?

2014-08-25 21:30:49 +0100 commented answer How do I invoke IPython from a macro?

@hunteke Well, I could use Python two in IPython, but I use it just for developement, once the macro is made, I need to run it from inside Libreoffice and it uses python 3 by default If I am not mistaken.

Karolus: Thanks, that works!

2014-08-25 19:24:35 +0100 marked best answer How do I invoke IPython from a macro?

I would like to be able to use an interactive shell for developing a macro. The answe to this question: http://ask.libreoffice.org/en/questio... suggests one needs only to include

from IPython import embed as II
II()

in the macro, but that does not work and throws this error:

com.sun.star.uno.RuntimeExceptionError during invoking function macro1 in module file:///home/drew/.config/libreoffice/4/user/Scripts/python/my_macro_module.py (<class 'KeyError'>: 'ooo_script_framework'
  /usr/lib/python3/dist-packages/IPython/terminal/embed.py:211 in function mainloop() [module = sys.modules[global_ns['__name__']]]
  /usr/lib/python3/dist-packages/IPython/terminal/embed.py:158 in function __call__() [global_ns=global_ns, compile_flags=compile_flags)]
  /usr/lib/python3/dist-packages/IPython/terminal/embed.py:301 in function embed() [shell(header=header, stack_depth=2, compile_flags=compile_flags)]
  /home/drew/.config/libreoffice/4/user/Scripts/python/my_macro_module.py:10 in function macro1() [embed()]
  /usr/lib/libreoffice/program/pythonscript.py:869 in function invoke() [ret = self.func( *args )]
)

How can I make it work?