Adding images to calc using python code

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
rag
Posts: 8
Joined: Thu Nov 12, 2015 4:11 pm

Adding images to calc using python code

Post by rag »

Hi Friends,

Would someone please let me know about adding an image to calc sheet. It would be more helpful if you provide some snippet of code using python.
Is it possible to do this using pyuno danny libraries?

Thanks in Advance,
Rag
open office 4.1.1 on windows 7
mauriciobaeza
Posts: 56
Joined: Thu Apr 22, 2010 5:03 am

Re: Adding images to calc using python code

Post by mauriciobaeza »

Try with...

Code: Select all

def insert_image():
    from com.sun.star.awt import Size

    size = Size()
    path = uno.systemPathToFileUrl('/home/mau/Pictures/foro.png')
    doc = XSCRIPTCONTEXT.getDocument()
    draw_page = doc.getCurrentController().getActiveSheet().getDrawPage()
    image = doc.createInstance( 'com.sun.star.drawing.GraphicObjectShape')
    image.GraphicURL = path
    draw_page.add(image)
    size.Width = 10000
    size.Height = 7500
    image.setSize(size)
    return
Best regards
______________________________________________
Everything not given is lost
AOO 4.1 / LibO 4.3 on ArchLinux with Gnome3
Please, I do not answer private questions, you use the forum
rag
Posts: 8
Joined: Thu Nov 12, 2015 4:11 pm

Re: Adding images to calc using python code

Post by rag »

Thanks mauriciobaeza.

I tried to execute the code from open office installation directory. But getting following error. Where can I get the missing module?
C:\Program Files (x86)\OpenOffice 4\program>python.exe insert_image.py
Traceback (most recent call last):
File "insert_image.py", line 16, in <module>
insert_image()
File "insert_image.py", line 2, in insert_image
from com.sun.star.awt import Size
ImportError: No module named com.sun.star.awt

Thanks,
Rag
open office 4.1.1 on windows 7
rag
Posts: 8
Joined: Thu Nov 12, 2015 4:11 pm

Re: Adding images to calc using python code

Post by rag »

I tried with the below code. It pasted the image name in to the cells instead of image.

def insert_image(file_path):
#from com.sun.star.awt import Size


#size = Size()
path = uno.systemPathToFileUrl('marbles.png')
#doc = XSCRIPTCONTEXT.getDocument()
url = unohelper.systemPathToFileUrl(os.path.abspath(file_path))
oDoc = StarDesktop.loadComponentFromURL(url, "_blank", 0, ())
draw_page = oDoc.getCurrentController().getActiveSheet().getDrawPage()
image = oDoc.createInstance('com.sun.star.drawing.GraphicObjectShape')
image.GraphicURL = path
draw_page.add(image)
# size.Width = 10000
# size.Height = 7500
# image.setSize(size)
return

import uno
import unohelper
import os.path
import argparse
import sys
from OOoLib import *
parser = argparse.ArgumentParser()
parser.add_argument('--file_path')
args = parser.parse_args(sys.argv[1:])
insert_image(args.file_path)
open office 4.1.1 on windows 7
User avatar
karolus
Volunteer
Posts: 1159
Joined: Sat Jul 02, 2011 9:47 am

Re: Adding images to calc using python code

Post by karolus »

You're running a python-process completly independent from soffice, it "knows" nothing about all this com.sun.star... stuff, until you do:

Code: Select all

import uno
first.

Code: Select all

import uno
from com.sun.star.awt import Size

def insert_image_calc():
    grafik_path = "......" # your path to some picture.[png|jpg|?]
    grafik_url = uno.systemPathToFileUrl( grafik_path ) # transition to URL
    doc = XSCRIPTCONTEXT.getDocument()
    sel = doc.CurrentSelection #choose cell at cursor position
    position = sel.Postion
    sheet = sel.Spreadsheet
    drawpage = sheet.DrawPage
    grafik = doc.createInstance("com.sun.star.drawing.GraphicObjectShape")
    grafik.GraphicURL = grafik_url
    grafik.Position = position
    size = Size()
    size.Height = 3500 #3.5 cm
    size.Width = 3800
    grafik.setSize(size)
    drawpage.add(grafik) 
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
rag
Posts: 8
Joined: Thu Nov 12, 2015 4:11 pm

Re: Adding images to calc using python code

Post by rag »

Thanks karolus. It is is pasting the image now. But it is not saving the image in calc sheet if I use "storeAsURL". Please find the below code. Normal data was saving fine with "oDoc.storeAsURL(url, ())". But it is not saving images, May be I need to give extra arguments to save the images. Please let me know if you have any idea about it. Whenever I close the calc document and reopen it again, I am not seeing the image. May I know what am I missing here? But I observed that the size of the file is increased, it means image is attached but not able to view it when it is reopened.

Code: Select all

def paste_image(file_path, sheet_number, image_path):
    import uno
    from com.sun.star.awt import Size
    grafik_path = os.path.abspath(image_path)
    grafik_url = uno.systemPathToFileUrl(grafik_path)
    url = unohelper.systemPathToFileUrl(os.path.abspath(file_path))
    oDoc = StarDesktop.loadComponentFromURL(url, "_blank", 0, ())
    sheet = oDoc.getSheets().getByIndex(int(sheet_number)-1)
    draw_page = sheet.DrawPage
    grafik = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
    grafik.GraphicURL = grafik_url
    size = Size()
    size.Height = 9500
    size.Width = 9800
    grafik.setSize(size)
    draw_page.add(grafik)
    oDoc.storeAsURL(url, ())
    oDoc.close(True)

import unohelper
import os.path
import argparse
import sys
from OOoLib import *
parser = argparse.ArgumentParser()
parser.add_argument('--file_path')
parser.add_argument('--sheet_number')
parser.add_argument('--image_path')
args = parser.parse_args(sys.argv[1:])
paste_image(args.file_path, args.sheet_number, args.image_path)
open office 4.1.1 on windows 7
User avatar
karolus
Volunteer
Posts: 1159
Joined: Sat Jul 02, 2011 9:47 am

Re: Adding images to calc using python code

Post by karolus »

Hallo

Should work with simply:

Code: Select all

    oDoc.store() #instead oDoc.storeAsUrl(... 
btw. its a strong rule in python to do the imports first, and on module-level but not inside functions, except you have some reasons to break the rule.
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
rag
Posts: 8
Joined: Thu Nov 12, 2015 4:11 pm

Re: Adding images to calc using python code

Post by rag »

Hi karolus,

Thanks for your reply. But it is not working with oDoc.srore() also. Observing same behavior. It is pasting initially but when document is closed and opened again not able to see the pasted image.

Thanks,
Rag
open office 4.1.1 on windows 7
mauriciobaeza
Posts: 56
Joined: Thu Apr 22, 2010 5:03 am

Re: Adding images to calc using python code

Post by mauriciobaeza »

You need, embedded image into document.

Code: Select all

import uno

CTX = uno.getComponentContext()
SM = CTX.getServiceManager()

def _create_instance(name, with_context=True):
    if with_context:
        instance = SM.createInstanceWithContext(name, CTX)
    else:
        instance = SM.createInstance(name)
    return instance

def path_to_url(path):
    if not path.startswith('file://'):
        path = uno.systemPathToFileUrl(path)
    return path

def insert_image():
    from com.sun.star.awt import Size
    from com.sun.star.beans import PropertyValue

    gp = _create_instance('com.sun.star.graphic.GraphicProvider')
    doc = XSCRIPTCONTEXT.getDocument()
    draw_page = doc.getCurrentController().getActiveSheet().getDrawPage()
    image = doc.createInstance( 'com.sun.star.drawing.GraphicObjectShape')
    draw_page.add(image)

    pv = PropertyValue()
    pv.Name = 'URL'
    pv.Value = path_to_url('/home/mau/Pictures/foro.png')
    image.Graphic = gp.queryGraphic((pv,))
    size = Size()
    size.Width = 10000
    size.Height = 7500
    image.setSize(size)
    return
Best regards.
______________________________________________
Everything not given is lost
AOO 4.1 / LibO 4.3 on ArchLinux with Gnome3
Please, I do not answer private questions, you use the forum
rag
Posts: 8
Joined: Thu Nov 12, 2015 4:11 pm

Re: Adding images to calc using python code

Post by rag »

Hi mauriciobaeza,

Thanks for your reply but It is not working for me and did not find any major difference with the code I have pasted.

Thanks,
Rag
open office 4.1.1 on windows 7
mauriciobaeza
Posts: 56
Joined: Thu Apr 22, 2010 5:03 am

Re: Adding images to calc using python code

Post by mauriciobaeza »

Wow, not different!!...
foro2.png
Two code work fine for me, the first insert image like link, and second code insert image embebbed.
______________________________________________
Everything not given is lost
AOO 4.1 / LibO 4.3 on ArchLinux with Gnome3
Please, I do not answer private questions, you use the forum
rag
Posts: 8
Joined: Thu Nov 12, 2015 4:11 pm

Re: Adding images to calc using python code

Post by rag »

My intention of non-working is the below problem which I mentioned above in the forum.

"It is is pasting the image now. But it is not saving the image in calc sheet if I use "storeAsURL". Please find the below code. Normal data was saving fine with "oDoc.storeAsURL(url, ())". But it is not saving images, May be I need to give extra arguments to save the images. Please let me know if you have any idea about it. Whenever I close the calc document and reopen it again, I am not seeing the image. May I know what am I missing here? But I observed that the size of the file is increased, it means image is attached but not able to view it when it is reopened."

I am running the code from C:\program_files\open office\program directory. So, your code related to getting document object etc. is not working for me. If I get document object as I did in my code, then it is working for me. In your latest code "_create_instance" is not working for me.
open office 4.1.1 on windows 7
Shantanu24
Posts: 1
Joined: Sat Dec 31, 2016 8:30 am

Re: Adding images to calc using python code

Post by Shantanu24 »

Hi. I also have to do the same thing. Send an image (.png) file to LibreOffice Calc. I have seen the code posted above and it compiles and runs with no error. But I have a very silly Q. The image is getting added to a Calc (.ods) file. BUT where is that Calc file? I cant seem to locate it.

When i put this line in the code, it still doesn't show any change

doc = XSCRIPTCONTEXT.getDocument('home/pi/abc.ods')

I created a abc.ods file and passed it into the code. But the ods file has no change after i run the code
OpenOffice 2.4 on Ubuntu 9.04
Post Reply