【问题标题】:How to script Visual Studio 2008 from Python?如何从 Python 编写 Visual Studio 2008 脚本?
【发布时间】:2008-10-21 16:08:55
【问题描述】:

我想编写驱动 Visual Studio 2008 和 Visual C++ 2008 的 Python 脚本。到目前为止我找到的所有示例都使用 win32com.client.Dispatch。这适用于 Excel 2007 和 Word 2007,但不适用于 Visual Studio 2008:

import win32com.client
app1 = win32com.client.Dispatch( 'Excel.Application' ) # ok
app2 = win32com.client.Dispatch( 'Word.Application' )  # ok
app3 = win32com.client.Dispatch( 'MSDev.Application' ) # error

有什么想法吗? Visual Studio 2008 是否使用不同的字符串来标识自己?上面的方法过时了吗?

【问题讨论】:

    标签: python visual-studio visual-studio-2008 visual-c++


    【解决方案1】:

    我不知道这是否会对 2008 年的您有所帮助,但使用 Visual Studio 2005 和 win32com 我能够做到这一点:

    >>> import win32com.client
    >>> b = win32com.client.Dispatch('VisualStudio.DTE')
    >>> b
    <COMObject VisualStudio.DTE>
    >>> b.name
    u'Microsoft Visual Studio'
    >>> b.Version
    u'8.0'
    

    不幸的是,我没有 2008 年可供测试。

    【讨论】:

    • 没问题。很高兴我能帮上忙。
    【解决方案2】:

    根据您的具体目标,AutoIt 可能会满足您的需求。事实上,我确信它会做任何你需要它做的事情。

    取自我的other post 关于如何在 Python 中使用 AutoIt:

    import win32com.client
    oAutoItX = win32com.client.Dispatch( "AutoItX3.Control" )
    
    oAutoItX.Opt("WinTitleMatchMode", 2) #Match text anywhere in a window title
    
    width = oAutoItX.WinGetClientSizeWidth("Firefox")
    height = oAutoItX.WinGetClientSizeHeight("Firefox")
    
    print width, height
    

    您当然可以使用任何AutoItX functions(请注意,该链接指向 AutoIt 函数参考,AutoIt 的 com 版本 - AutoItX 具有该列表的子集...文档包含在下载中)这样。我不知道你想做什么,所以我无法为你指出适当的功能,但这应该可以帮助你入门。

    【讨论】:

      【解决方案3】:

      ryan_s 有正确的答案。您可能会重新考虑使用 win32com。

      我更喜欢 comtypes 模块而不是 win32com。一般来说,它更适合 ctypes 和 python。

      在 vs 2008 中使用任何一种方法都可以。这是一个打印 Visual Studio 中所有命令的名称和键盘快捷键的示例。

      import comtypes.client as client
      
      vs = client.CreateObject('VisualStudio.DTE')
      
      commands = [command for command in vs.Commands if bool(command.Name) or bool(command.Bindings)]
      commands.sort(key=lambda cmd : cmd.Name)
      
      f= open('bindings.csv','w')
      
      for command in commands:
          f.write(command.Name+"," +";".join(command.Bindings)+ "\n")
      
      f.close()
      

      【讨论】:

        【解决方案4】:

        您可以尝试 .Net 自己的版本,IronPython。 它有一个 VS 插件,IronPythonStudio

        作为一种.Net 语言,您可以访问所有可用的程序集,包括Visual Studio Tools for Office

        【讨论】:

          【解决方案5】:

          截至 2013 年,更好的选择是通过 IronPython 编写 Visual Studio 脚本,因为 CLR/COM 和其他 MS 东西集成更好:

          
          import clr
          import System
          
          t = System.Type.GetTypeFromProgID("AutoItX3.Control")
          oAutoItX = System.Activator.CreateInstance(t)
          
          oAutoItX.Opt("WinTitleMatchMode", 2)
          
          width = oAutoItX.WinGetClientSizeWidth("IronPythonApplication1 - Microsoft Visual Studio (Administrator)")
          height = oAutoItX.WinGetClientSizeHeight("IronPythonApplication1 - Microsoft Visual Studio (Administrator)")
          
          print width, height
          

          【讨论】:

            猜你喜欢
            • 2010-09-07
            • 2010-10-24
            • 2010-09-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多