GoldenRoses
GoldenRoses

Reputation: 31

How do I run a libreoffice macro from the command line without GUI?

Introduction

I have a docker container with a GUI in which I registered my macro through LibreOffice Calc. Using the GUI given by the docker container, I can successfully run the macro via the command line. When I load the image in a Kubernetes Pod, however, the macro hangs indefinitely when I try to run it.

Here is the macro:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Module1" script:language="StarBasic">REM  *****  BASIC  *****

Sub FitToPage
    Dim document As Object, pageStyles As Object
    document = ThisComponent
    pageStyles = document.StyleFamilies.getByName(&quot;PageStyles&quot;)
    For i = 0 To Document.Sheets.Count - 1
        Dim sheet As Object, Style As Object
        sheet = document.Sheets(i)
        style = pageStyles.getByName(sheet.PageStyle)
        style.ScaleToPagesX = 1
        style.ScaleToPagesY = 999
    Next
    On Error Resume Next
    document.storeSelf(Array())
    document.close(true)
End Sub
</script:module>

Command to run macro:

soffice --headless --nologo --nofirststartwizard --norestore macro://Standard.Module1.FitToPage <file>.xlsx

The strange thing is, I am able to run other headless libreoffice commands just fine. For example, if I were to try to just convert the file to a pdf without changing the scaling using soffice --headless --nologo --nofirststartwizard --norestore --convert-to pdf --outdir . <file>.xlsx, then soffice runs perfectly fine.

Other Information:

Conclusion

Any help at all would be much appreciated. I can provide any more information required upon request.

UPDATE

For me, running soffice & to run it in the background before running the macro seemed to work. It might have something to do with how libreoffice handles its state?

This isn't a perfect solution by any means, so I'm going to leave it open in case someone has a better solution.

Upvotes: 1

Views: 2877

Answers (1)

X Tian
X Tian

Reputation: 772

Your call of the macro is missing a slash /.

The command line should be

    $ soffice --headless --nologo --nofirststartwizard --norestore macro:///Standard.Module1.FitToPage <file>.xlsx 

Running in headless mode will obscure any security messages about running macros, simply removing --headless allows you to check if there are any security messages possibly about running unknown macros that you need to authorsise, before a --headless run will proceed and complete.

Upvotes: 1

Related Questions