1

I have some jpegs I want to use as background pictures in a libreOffice Impress presentation. But I don't like to add them slide by slide.

Is there a way to use the (Linux) commandline to add the pics into a presentation file?

Something like:

for i in *.jpg; do <add a new slide with that jpg as background to the presentation file>; done
schmendrich
  • 111
  • 1

1 Answers1

2

In LibreOffice, go to Tools -> Macros -> Organize Macros -> LibreOffice Basic and add the following macro to Module1.

Sub InsertSlideWithBackgroundImage(imageFilePath)
    imageFileURL = ConvertToURL(imageFilePath)
    oDoc = ThisComponent
    oBackground = oDoc.createInstance("com.sun.star.drawing.Background")
    oBitmaps = ThisComponent.createInstance( "com.sun.star.drawing.BitmapTable")
    iBitmap = 1
    Do
        sBitmapName = "bk" & iBitmap
        iBitmap = iBitmap + 1
    Loop Until Not oBitmaps.hasByName(sBitmapName)
    oBitmaps.insertByName(sBitmapName, imageFileURL)
    oBackground.FillStyle = com.sun.star.drawing.FillStyle.BITMAP
    oBackground.FillBitmapURL = oBitmaps.getByName(sBitmapName)
    oSlideList = oDoc.getDrawPages()
    oSlide = oSlideList.insertNewByIndex(oSlideList.Count)
    oSlide.Background = oBackground
End Sub

Then add an image from the command line like this.

loimpress "file.odp" "macro:///Standard.Module1.InsertSlideWithBackgroundImage(image.jpg)"

Alternatively, run python3 from the command line and interact with a listening instance of LibreOffice. This approach is explained at http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html.

EDIT:

To run it directly, pass the file path of the image.

Sub Test1
    Call InsertSlideWithBackgroundImage("/path/to/your_image.jpg")
End Sub
Jim K
  • 12,824
  • 2
  • 22
  • 51
  • It worked when I tested it. Please give more details. Were you able to run the macro directly via *Tools -> Macros -> Run Macro*? Did it partly work, or give an error message? If it worked when running the macro directly, then what happened when running from the command line? – Jim K Jan 25 '18 at 10:25
  • Error Message after running directly: wrong number of parameters – schmendrich Jan 29 '18 at 08:02
  • @JimK I get the error: BASIC runtime error. Property or method not found: createInstance. – Carlos May 15 '23 at 09:23
  • 1
    @Carlos: [FillBitmapURL](https://api.libreoffice.org/docs/idl/ref/servicecom_1_1sun_1_1star_1_1drawing_1_1FillProperties.html#a571ff3a98002a532602bd88a6fff4f3c) is now deprecated. It worked for me after changing that line to `FillBitmap` instead. However, I do not know how you got the `createInstance` error — post a new question with details explaining how to reproduce your error, including a link to this question. – Jim K May 15 '23 at 15:34
  • @JimK The reason for `createInstance not found error` is that I directly ran marco without creating a presentation, forgive my unfamiliarity with LibreOffice. I've got another question: I check the slide properties of background after running the marco and find that the style is `tiled` by default which does not fill the whole slide, how can I add the background with a `stretched` style? – Carlos May 16 '23 at 02:17
  • @Carlos: This is explained in the API docs page linked in my previous comment. – Jim K May 16 '23 at 19:52
  • @JimK Following the docs, it finally worked after adding a line of code `oBackground.FillBitmapMode = com.sun.star.drawing.BitmapMode.STRETCH`. – Carlos May 17 '23 at 06:52