GeoProc.com
GeoProc.com

LibreOffice writer: Save all images to files

macros

LibreOffice Writer macro to save all the images in the document to separate files. Filenames are automatically generated from image name/ID. All images are created in user/Pictures directory by default.

Installation

Copy following code snippet into clipboard.


Sub SaveIMGs
' Save all images in the document to .png files
' in user's %HOME%/Pictures directory

Dim MP(1) As New com.sun.star.beans.PropertyValue
Dim oDoc As Object
Dim oGraphics As Object
Dim oProv As Object
Dim oImg As Object

   oProv = createUnoService("com.sun.star.graphic.GraphicProvider")
   oDoc = ThisComponent
   oGraphics = oDoc.getGraphicObjects()

   hd = Environ("HOMEPATH")
   If InStr(hd, "\") > 0 Then
      'Windows
      hd = "/C:" + join(split(hd, "\"), "/")
   ELSE
      'Linux/MacOS
      hd = Environ("HOME")
   End If

   If hd = "" Then
      MsgBox "ERROR: Cannot locate user's home directory!"
   Else
      For Each oImg in oGraphics
         MP(0).Name = "URL"
         MP(0).Value = "file://" & hd & "/Pictures/" & oImg.Name & ".png"
         MP(1).Name = "MimeType"
         MP(1).Value = "image/png"
         oProv.storeGraphic(oImg.Graphic, MP)
      Next
   End If

End Sub

How to use

  1. Open the document containing the images to be saved.
  2. Then Tools -> Macros -> Run Macro...
    And expand the tree to the following:
    macros
  3. Select SaveIMGs and click Run
  4. All images are created in user/Pictures directory by default.

Licence

This macro is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This macro is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Copy of the GNU General Public License is availabel at: http://www.gnu.org/licenses/

 

Published date: 03 Sep 2020.