开发者

Call Word.SaveAs method from IronPython

I used to manipulate Word from C# and now I test it with IronPython. Here is my code :

import clr

clr.AddReference('System')
clr.AddReference('mscorlib')
clr.AddReference('Microsoft.Office.Interop.Word')
from System.Reflection import Missing
from Microsoft.Office.Interop.Word import ApplicationClass

missing = Missing.Value

word = ApplicationClass()
word.Visible = True
doc = word.Documents.Add(missing, missing, missing, missing)

doc_file_name = r"C:\MyWord.docx"
doc.SaveAs(doc_file_name, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
doc.Close(missing, missing, missing)
WordApp.Quit(missing, missing, missing)

The ligne with the SaveAs call causes an error : ValueError: Could not convert argument 0 for call to Save开发者_开发技巧As.

Did anyone encounter the same problem or have a solution ?


I think the problem is with the value of file type and if your instance of word is in compatibility mode or not.

I made these changes and it saved fine.

doc_file_name = r"C:\MyWord.doc"
doc.SaveAs(doc_file_name)

This change bombed out.

doc_file_name = r"C:\MyWord.docx"
doc.SaveAs(doc_file_name)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜