【问题标题】:Call Word.SaveAs method from IronPython从 IronPython 调用 Word.SaveAs 方法
【发布时间】:2011-05-06 12:08:10
【问题描述】:

我曾经在 C# 中操作 Word,现在我使用 IronPython 对其进行测试。 这是我的代码:

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)

使用 SaveAs 调用导致错误:ValueError: Could not convert argument 0 for call to SaveAs.

有没有人遇到同样的问题或有解决办法?

【问题讨论】:

    标签: .net ms-word ironpython


    【解决方案1】:

    我认为问题在于文件类型的值以及您的 word 实例是否处于兼容模式。

    我做了这些更改,它保存得很好。

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

    这个变化被轰炸了。

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

    【讨论】:

    • 非常感谢,它有效。但是你知道为什么我不能对所有参数都使用 SaveAs 方法吗?
    • 仅当您有实际值时。我发现(来自 vba)最好只指定您需要的内容并让对象处理默认值。
    猜你喜欢
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多