[Solved] How to save all images from SpreadSheet?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ACTPOHOM
Posts: 4
Joined: Wed Jun 14, 2017 11:56 am

[Solved] How to save all images from SpreadSheet?

Post by ACTPOHOM »

Hi all!
how to save all images in jpg and replacement of graphics to the file name?

img_01.jpg
img_02.jpg
etc

p.s. MacOS
Last edited by Hagar Delest on Thu Jun 15, 2017 1:26 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 4.1.3 / MacOS 10.11.6
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to save all images from SpreadSheet?

Post by Villeroy »

An ODF document is just a zip archive. On my Linux system I can do the following:

Code: Select all

unzip docname.ods Pictures/*
this extracts the document's Pictures folder.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
ACTPOHOM
Posts: 4
Joined: Wed Jun 14, 2017 11:56 am

Re: How to save all images from SpreadSheet?

Post by ACTPOHOM »

need save Files and replace pictures in sells to filenames

for Excel i have VB script, but in OpenOffice not working


Code: Select all

Sub Save_Object_As_Picture()
    Dim li As Long, oObj As Shape, wsSh As Worksheet, wsTmpSh As Worksheet
    Dim sImagesPath As String, sName As String
   
    sImagesPath = ActiveWorkbook.Path & "/images/" '"
    If Dir(sImagesPath, 16) = "" Then
        MkDir sImagesPath
    End If
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Set wsSh = ActiveSheet
    Set wsTmpSh = ActiveWorkbook.Sheets.Add
    For Each oObj In wsSh.Shapes
        If oObj.Type = 13 Then
            li = li + 1
            oObj.Copy
            sName = "img" & li
            With wsTmpSh.ChartObjects.Add(0, 0, oObj.Width, oObj.Height).Chart
                .ChartArea.Border.LineStyle = 0
                .Parent.Select
                .Paste
                .Export Filename:=sImagesPath & sName & ".jpg", FilterName:="JPG"
                .Parent.Delete
            End With
            oObj.TopLeftCell.Value = sName
            oObj.Delete '????_?? ???????? ? ?????
        End If
    Next oObj
    Set oObj = Nothing: Set wsSh = Nothing
    wsTmpSh.Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "??????? ????????? ? ?????: " & sImagesPath, vbInformation, "www.excel-vba.ru"
End Sub

Image
OpenOffice 4.1.3 / MacOS 10.11.6
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: How to save all images from SpreadSheet?

Post by hubert lambert »

Give this a try:

Code: Select all

sub export_replace_img
	doc = thiscomponent
	a_url = split(doc.URL, "/")

	GP = createUnoService("com.sun.star.graphic.GraphicProvider")
	dim props(1) as new com.sun.star.beans.PropertyValue
	props(0).Name = "URL"
	props(1).Name = "MimeType"

	for each sheet in doc.Sheets
		drawpage = sheet.DrawPage
		for n = drawpage.Count-1 to 0 step -1
			elem = drawpage.getByIndex(n)
			if elem.supportsService("com.sun.star.drawing.GraphicObjectShape") then
				if elem.Graphic.MimeType = "image/jpeg" then
					a_url(ubound(a_url)) =  elem.Name & ".jpg"
					props(0).Value = convertToURL(join(a_url, "/"))
					props(1).Value = elem.Graphic.MimeType
					GP.storeGraphic(elem.Graphic, props)
					on error resume next	'image could be anchored to page
					elem.Anchor.setString(elem.Name)
					drawpage.remove(elem)
				end if
			end if		
		next n
	next sheet
	msgbox "done"
end sub
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
ACTPOHOM
Posts: 4
Joined: Wed Jun 14, 2017 11:56 am

Re: How to save all images from SpreadSheet?

Post by ACTPOHOM »

Not working...
Maybe the original file will help ?
https://drive.google.com/open?id=0B87VT ... TROV0ZGb0k
OpenOffice 4.1.3 / MacOS 10.11.6
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: How to save all images from SpreadSheet?

Post by hubert lambert »

Checking for image type was useless. Here is a working example:

Code: Select all

sub export_replace_img
   doc = thiscomponent
   a_url = split(doc.URL, "/")

   GP = createUnoService("com.sun.star.graphic.GraphicProvider")
   dim props(1) as new com.sun.star.beans.PropertyValue
   props(0).Name = "URL"
   props(1).Name = "MimeType"

   for each sheet in doc.Sheets
      drawpage = sheet.DrawPage
      for n = drawpage.Count-1 to 0 step -1
         elem = drawpage.getByIndex(n)
         if elem.supportsService("com.sun.star.drawing.GraphicObjectShape") then
	        a_url(ubound(a_url)) =  elem.Name & ".jpg"
	        props(0).Value = convertToURL(join(a_url, "/"))
	        props(1).Value = elem.Graphic.MimeType
	        GP.storeGraphic(elem.Graphic, props)
	        on error resume next   'image could be anchored to page
	        elem.Anchor.setString(elem.Name)
	        drawpage.remove(elem)
         end if      
      next n
   next sheet
   msgbox "done"
end sub
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
ACTPOHOM
Posts: 4
Joined: Wed Jun 14, 2017 11:56 am

Re: How to save all images from SpreadSheet?

Post by ACTPOHOM »

Works !
thank you very much!
and wish that the names were appointed to force Img_xx.jpg ?
OpenOffice 4.1.3 / MacOS 10.11.6
Post Reply