[IronPython] Word and MissingMemberException, 'tuple' object has no attribute 'SaveAs
Martin Maly
Martin.Maly at microsoft.com
Tue Oct 10 03:28:51 CEST 2006
The problem is that app.Documents.Open returns multiple values (in addition to regular return value, there are others either via ref or out parameters). This is what IronPython translates into tuples. This is quite common construct in Python:
def multiple():
return 1, "hello", 4.5
i, s, f = multiple()
Similarly, you can capture the multiple return values from Word's Open to a tuple and then inspect its elements to find out which one is the actual document. It appears from the printout below, that it is the one at index zero. Therefore, you can write:
values = app.Documents.Open( ... );
myDoc = values[0]
Martin
-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Frey576
Sent: Saturday, October 07, 2006 2:48 PM
To: users at lists.ironpython.com
Subject: [IronPython] Word and MissingMemberException, 'tuple' object has no attribute 'SaveAs
Hi,
I am new to IronPython but have a couple of years experience with Python.
Currently I am stuck with a problem trying to open a Word document (WordML)
and saving it as .rtf back to disk
The exception I get upon call of myDoc.SaveAs is:
MissingMemberException, 'tuple' object has no attribute 'SaveAs'
when I check myDoc (obtained from Word.Documents.Open) in the CLR debugger I
see indeed that I got a tuple
but it does not look like a Python tuple and I dont know how to handle that:
- myDoc {(<DocumentClass object at 0x000000000000002B>,
'D:\\Projekte\\SGMLtoRtf\\dirWatch_SGMLtoRtf\\test\\HelloWorld.xml',
<Missing object at 0x000000000000002C>, True, <Missing object at
0x000000000000002C>, <Missing object at 0x000000000000002C>, <Missing object
at 0x000000000000002C>, <Missing object at 0x000000000000002C>, <Missing
object at 0x000000000000002C>, <Missing object at 0x000000000000002C>,
<Missing object at 0x000000000000002C>, <Missing object at
0x000000000000002C>, <Missing object at 0x000000000000002C>, <Missing object
at 0x000000000000002C>, <Missing object at 0x000000000000002C>, <Missing
object at 0x000000000000002C>, <Missing object at 0x000000000000002C>)}
object {IronPython.Runtime.Tuple}
so the type is IronPython.Runtime.Tuple
What is wrong with using Documents.Open from IronPython?
My script, stripped down a little bit, is:
import System.Type
import clr
clr.AddReferenceByPartialName("Microsoft.Office.Interop.Word")
import Microsoft.Office.Interop.Word as Word # ApplicationClass,
WdSaveFormat
import sys
sys.path.append(r"c:\python24\lib")
import os
inPath = r".\test\HelloWorld.xml"
inPath = os.path.abspath(inPath)
assert os.path.isfile(inPath), "not found: %s" % inPath
outPath = os.path.splitext(inPath)[0] +".rtf"
app = Word.ApplicationClass()
try:
readOnly = True
myDoc = app.Documents.Open(inPath, System.Type.Missing, readOnly,
*[System.Type.Missing]*13);
myDoc.SaveAs( outPath,
Word.WdSaveFormat.wdFormatRTF, # FileFormat
System.Type.Missing, # LockComments
System.Type.Missing, # Password
False, # AddToRecentFiles
System.Type.Missing, # WritePassword
System.Type.Missing, # ReadOnlyRecommended
System.Type.Missing, # EmbedTrueTypeFonts
System.Type.Missing, # SaveNativePictureFormat
System.Type.Missing, # SaveFormsData
System.Type.Missing, # SaveAsAOCELetter
System.Type.Missing, # Encoding
System.Type.Missing, # InsertLineBreaks
System.Type.Missing, # AllowSubstitutions
System.Type.Missing, # LineEnding
System.Type.Missing, # AddBiDiMarks
)
myDoc.Close(
False, # SaveChanges
System.Type.Missing, # OriginalFormat
System.Type.Missing, # RouteDocument
)
finally:
app.Quit(*[System.Type.Missing]*3)
How can I call member functions of a Word document obtained from
Documents.Open?
Peter
--
View this message in context: http://www.nabble.com/Word-and-MissingMemberException%2C-%27tuple%27-object-has-no-attribute-%27SaveAs-tf2402444.html#a6698479
Sent from the IronPython mailing list archive at Nabble.com.
_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
More information about the Ironpython-users
mailing list