How to remove all the images in a document in LibreOffice Writer?

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.


Solution 1:

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.

Solution 2:

I'm assuming that the text of the document is formatted and that saving as text-only isn't ideal. In that case, one could take a somewhat longish approach:

  • Create a temporary folder.

  • Open the document in LibreOffice normally.

  • Choose Save As and here select HTML Document (Writer) (.html) as the format.

    • Also, specify the temporary folder for the destination and give the proposed html file a suitable name. Agree to Save as html and not odt format in the next screen.
  • Close LibreOffice.

  • Open the temporary folder created above. You should see one html file and a bunch of images.

    • Delete the images if they're not needed further.
  • Then, open the .html file in an appropriate text editor to strip out the tags that would otherwise, if left in, cause gaps in the document.

    • For example, in Geany, I used <IMG SRC="[^>]+> as the search expression and nothing in the "replace with" box.

geany

  • Save the .html file. Now, when viewed in a browser or even in LibreOffice, there will be no images, no placeholders, just formatted text.

Edit: Instead of using a GUI-based text editor, assuming one is in the relevant folder and that cli.html is the name given while saving to html in LibreOffice, one could just use the command line this way:

perl -p -i.bak -e 's/<IMG[^>]+>//g' cli.html

A backup file, cli.html.bak, is created just in case ...

Solution 3:

You can select one image in document via mouse, then press Ctrl + A for select all images in current Sheet, and Del for delete them.