Ubuntu – How to remove all the images in a document in LibreOffice Writer

libreoffice

Ubuntu 12.04

I want to remove all the images from a document in LibreOffice Writer. My document contains ~ 350 images. Is there a way to delete them en masse rather than one by one? I tried unchecking Tools > Options > LibreOffice Writer > View > Display > "Graphics and objects". But the place-holders are still visible.

Edit: I know about saving the page as a plain text file, But I need formatting in my document.

Best Answer

You can also use a short macro from within LibreOffice to remove all images in a document:

Sub RemoveImages

   Dim oDoc as Object
   oDoc = ThisComponent

   Dim oGraphics as Object
   oGraphics = oDoc.getGraphicObjects()

   Dim oImg as Object

   For Each oImg in oGraphics
       oDoc.getText().removeTextContent(oImg)
   Next 

End Sub

This example could also be modified to change the properties of images (such as making them all a uniform size) as well as handling shape objects, etc.

Creating Macros

LibreOffice provides a Basic language as well as in IDE to create, debug, store, and run code.

To open the macro dialog, use Alt + F11 or, from the menu, Tools > Macros > Organize Macros > LibreOffice Basic

Code is placed in modules, which are organized into libraries. You can create your own, but for most purposes you can use the built-in MyMacros library and the built-in Module1.

Once the macro dialog is open, highlight Module1 and press Edit. This opens the IDE.

Code is organized into Subs and Functions. Borrowing from VBA, Functions are used to return a value and Subs do not return a value. You can define your own subs and functions anywhere below Main. So to use this code to remove images, you just paste it in the module.

Using Macros

You can run the code right from the IDE by clicking anywhere inside the Sub you want to run and press F5. (Functions, since they return a value, need to be called from a Sub.) You can also use the dialog buttons or menu to run the code. The IDE includes an integrated debugger, which is crucial when writing new code.

The next time you open the macro dialog, you will see the sub listed as a macro and it can then be run without opening the IDE. Individual macros can also be assigned to menu or toolbar items.

Note: This macro will run on whatever is the active document. When running a macro from the menu or macro dialog, this can be assumed to be the document where you just pressed the button; but when using the IDE to create, run, or debug code, make sure you haven't made another document the active document.

LibreOffice Basic

LibreOffice Basic is very similar to VBA, but that similarity can also be deceiving since the object model is completely different and the syntax also has many differences.

For example, in VBA, once you have a reference to an image object, the image object would have a delete method. Here, objects have or inherit very few methods. To delete the image object, you use the document element with a structure and syntax very similar to a browser-based DOM.

LibreOffice Help has links for getting started with LibreOffice Basic.