Author Topic: Using ActiveX Image control with pyxll  (Read 3457 times)

Vitaliy

  • Newbie
  • *
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Using ActiveX Image control with pyxll
« on: February 17, 2011, 08:36:30 AM »
Could please someone print the working code to output some picture/graph to Excel sheet?
I want to use matplotlib or pychardir to make some graphic file (in .png) and after that load it to ActiveX Image control. It could be somelike "click on button -> picture loaded".

Best,
Vitaliy

Tony

  • Administrator
  • Hero Member
  • *****
  • Posts: 606
  • Karma: +14/-1
    • View Profile
Re: Using ActiveX Image control with pyxll
« Reply #1 on: February 19, 2011, 03:46:10 PM »
Hi Vitally,

a good general approach I find useful is to use the Excel macro recorder to create some VBA to show how to do something, and then translate that to python code.

I recorded a macro of me loading an image, which gave the following VBA code:

Sub Macro1()
    ActiveSheet.Pictures.Insert( _
        "C:\Users\Public\Pictures\Sample Pictures\Koala.jpg").Select
End Sub

And then from that I wrote the following python code, using PyXLL:

@xl_menu("Load Image")
def load_image():
    xl_app().ActiveSheet.Pictures().Insert("C:\Users\Public\Pictures\Sample Pictures\Koala.jpg")

Note that I've used the xl_app function from the automation example to get the Excel Application object and I've missed off the call to Select as I just wanted to load the image and not select it.

You can also use the Object Browser in the Excel VBA editor to find the properties and methods of the Excel object model as it's the same in COM as it is in VBA.

This code works for Excel 2010, but I've not tested it with other versions. If it doesn't work, the method for getting code that does is the same.

I hope this helps,
Tony

Vitaliy

  • Newbie
  • *
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Re: Using ActiveX Image control with pyxll
« Reply #2 on: February 22, 2011, 08:23:14 AM »
Tony, it was very helpful. Thank you!
PS. Full code was:
--
@xl_menu("Load Image")
def load_image():
    xl_app().ActiveSheet.Pictures().Insert("C:\\Data\\a\\accR_2009-2011-02-15.png")
--