Embedding objects( txt, doc) into excel using python

G

Girish

Hi,

I want to embed a txt document into an excel using python.

Here is my code, but i get an error message
===================================================
Traceback (most recent call last):
File "C:\Documents and Settings\kusumap\Desktop\Girish.py", line 7,
in ?
worksheet.OLEObjects.Add(Filename="C:\Documents and Settings
\kusumap\My Documents\desktop.ini", Link=False,DisplayAsIcon=True,
IconFileName="packager.exe", IconIndex=0, IconLabel="C:\Documents and
Settings\kusumap\My Documents\desktop.ini").Select
AttributeError: 'function' object has no attribute 'Add'
===================================================

import win32com.client
ExcelApp = win32com.client.Dispatch("Excel.Application")
ExcelApp.visible = 1
workbook = ExcelApp.Workbooks.open("C:\Software\New Microsoft Excel
Worksheet.xls")
worksheet = workbook.Activesheet
#worksheet.OLEObjects.Add(Filename="C:\Documents and Settings\p\My
Documents\desk.txt", Link=False,DisplayAsIcon=True,
IconFileName="packager.exe", IconIndex=0, IconLabel="C:\Documents and
Settings\p\My Documents\desk.txt").Select

Can anyone please whtz the problem with the code and how to overcome
the same.

Thanks
Girish S
 
T

Tim Golden

Girish said:
I want to embed a txt document into an excel using python.

I didn't know people still did that! Still, each to
his own ;)
Here is my code, but i get an error message
===================================================
Traceback (most recent call last):
File "C:\Documents and Settings\kusumap\Desktop\Girish.py", line 7,
in ?
worksheet.OLEObjects.Add(Filename="C:\Documents and Settings
\kusumap\My Documents\desktop.ini", Link=False,DisplayAsIcon=True,
IconFileName="packager.exe", IconIndex=0, IconLabel="C:\Documents and
Settings\kusumap\My Documents\desktop.ini").Select
AttributeError: 'function' object has no attribute 'Add'
===================================================

Well done for providing reproducible code, by the way.
It's amazing how many people expect us to deduce code
from tracebacks! Two things:

1) You need to use raw strings for your path names
or to double-up the slashes. By good fortune, you
don't seem to be using any actual special characters
(\n, \r, \t etc.) but you won't always be so lucky!

2) As the error suggests, OLEObjects is in fact a method,
not an container. Slightly more concise screen dump:

<dump>
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more
information.<bound method _Worksheet.OLEObjects of
<win32com.gen_py.Microsoft Excel 11.0 Object
Library._Worksheet instance at 0x24646440>><win32com.gen_py.Microsoft Excel 11.0 Object
Library.OLEObjects instance at 0x24648240><bound method OLEObjects.Add of <win32com.gen_py.Microsoft
Excel 11.0 Object Library.OLEObjects instance at 0x24648240>></dump>

As you can see, the ws.OLEObjects is a bound method,
ie a function within the _Worksheet class. It seems
to return a value which is an OLEObjects instance,
which itself seems to have an .Add method. Although
I could forage in the docs, I just tried it out at
the interpreter. You can do the same if you want!

TJG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top