Picture in Excel issue

Steve Holden steve at holdenweb.com
Mon Oct 2 08:02:32 CEST 2006


Jim Chiang wrote:
> I’m trying to very simply insert a picture from a file into an excel 
> spreadsheet. I know how to do this in VBA and it works fine, however 
> when I try this from python I get an error. Doing a search on this 
> turned up nothing.
> 
> The code I’m using is:
> 
> from win32com.client.dynamic import Dispatch
> 
> xl = Dispatch( 'Excel.Application' )
> 
> xl.Visible=1
> 
> xl.Workbooks.Add()
> 
> xl.ActiveSheet.Pictures.Insert("C:\a.jpg")
> 
>  
> 
> Traceback (most recent call last):
> 
>   File "<pyshell#7>", line 1, in <module>
> 
>     xl.ActiveSheet.Pictures.Insert("C:\a.jpg")
> 
> AttributeError: 'function' object has no attribute 'Insert'
> 
>  
> 
> I’m not sure why I get this issue since 
> ‘ActiveSheet.Pictures.Insert("C:\a.jpg")’ works fine from within Excel. 
> Several internet posts from my searches also suggest to use this method.
> 
>  
> 
> I’ve tried this on both Python 2.1 and 2.5 with the same results.
> 
> Any idea what the problem is or how I can insert the picture??
> 
>  
> 
> TIA,
> 
> Jim
> 
>  
> 
I suspect that Pictures is a function that returns a collection - try

   ActiveSheet.Pictures().Insert("C:\a.jpg")

Remember that VBA uses implied function calls, you have to be explicit 
in Python.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list