Fill the background of a presentation file with pics via commandline

235 Views Asked by At

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
1

There are 1 best solutions below

7
On

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