Search code examples
pythonlibreofficelibreoffice-writer

libreoffice writer python script to set view layout columns


For text documents in LO writer I want a shortcut to toggle the view between 1 and 2 columns. To that end, I recorded two macros -- which had no effect -- so I'm trying with Python.

The Writer Menu.View.Zoom.Zoom menu item opens the Zoom and View Layout dialog. That dialog offers two panels: Zoom and View Layout

In a Python script we can access current_controller.view_settings.ZoomValue and ...ZoomType but I don't find a way to the View Layout values.

In the LO user config file registrymodifications.xcu we have these three lines:

 <item oor:path="/org.openoffice.Office.Writer/Layout/Zoom"><prop oor:name="Type" oor:op="fuse"><value>0</value></prop></item>
 <item oor:path="/org.openoffice.Office.Writer/Layout/Zoom"><prop oor:name="Value" oor:op="fuse"><value>120</value></prop></item>
 
 <item oor:path="/org.openoffice.Office.Writer/Layout/ViewLayout"><prop oor:name="Columns" oor:op="fuse"><value>1</value></prop></item>

Notice that the two zoom values are at the same level under Layout accessed in script directly through view_settings (see above). At that same level we have ViewLayout but in a script I have found no way of accessing what I would like to be view_settings.ViewLayout or view_settings.getProperty(...)


Solution

  • I haven't learned to use Python to control LO, but it should be possible to call the Dispatcher. I recorded a macro, but didn't find the ViewLayout being modified. Looking at information you provided, I came up with this, which worked to change to 3 columns.

    sub ViewLayout_3Col
        rem -----------------------------------------------
        rem define variables
        dim document   as object
        dim dispatcher as object
        rem -----------------------------------------------
        rem get access to the document
        document   = ThisComponent.CurrentController.Frame
        dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
        
        rem ----------------------------------------------
        dim args2(0) as new com.sun.star.beans.PropertyValue
        args2(0).Name = "ViewLayout.Columns"
        args2(0).Value = 3
        
        dispatcher.executeDispatch(document, ".uno:ViewLayout", "", 0, args2())
    end sub