简体   繁体   English

无法从Python 2.7 / 3.5访问Excel.Application()。Workbooks中的函数:'__ComObject'对象没有属性X

[英]Can't access functions in Excel.Application().Workbooks from Python 2.7/3.5: '__ComObject' object has no attribute X

I am trying to use Microsoft.Office.Interop.Excel from a Python script: 我正在尝试通过Python脚本使用Microsoft.Office.Interop.Excel:

import msvcrt
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
import Microsoft.Office.Interop.Excel as Excel

excel = Excel.ApplicationClass()
excel.Visible = True            # makes the Excel application visible to the user - will use this as True for debug
excel.DisplayAlerts = False     # turns off Excel alerts since I don't have a handler

print ("Excel: " + str(type(excel)))
print ("Workbooks: " + str(type(excel.Workbooks)))
print ("Workbooks count: " + str(excel.Workbooks.Count))
#wb = excel.Workbooks.Open(r'C:\Projects\Experiments\Python\ExcelInterop\Test.xlsx')

print ("Press any key")
msvcrt.getch()

Here is the output: 这是输出:

C:\Projects\Experiments\Python\ExcelInterop>exceltest.py
Excel: <class 'Microsoft.Office.Interop.Excel.ApplicationClass'>
Workbooks: <class 'System.__ComObject'>
Traceback (most recent call last):
  File "C:\Projects\Experiments\Python\ExcelInterop\exceltest.py", line 12, in <module>
    print ("Workbooks count: " + str(excel.Workbooks.Count))
AttributeError: '__ComObject' object has no attribute 'Count'
  • I am running in an admin cmd prompt on Windows 10 我在Windows 10的admin cmd提示符下运行
  • I have tried python 2.7 and 3.5 (using py -3 exceltest.py). 我已经尝试了python 2.7和3.5(使用py -3 exceltest.py)。
  • Microsoft.Office.Interop.Excel is version 15.0.4420.1017 (Office 2013) Microsoft.Office.Interop.Excel是15.0.4420.1017版本(Office 2013)
  • I created a similar .NET console app, which worked fine. 我创建了一个类似的.NET控制台应用程序,效果很好。
  • I used ILDASM to check references from Microsoft.Office.Interop.Excel, and then gacutil -l to double-check that all referenced assemblies are in the GAC. 我使用ILDASM检查来自Microsoft.Office.Interop.Excel的引用,然后使用gacutil -l再次检查所有引用的程序集是否在GAC中。
  • Just in case, I copied office.dll, stdole.dll and Microsoft.Vbe.Interop.dll into the folder where the Python script is running. 以防万一,我将office.dll,stdole.dll和Microsoft.Vbe.Interop.dll复制到了运行Python脚本的文件夹中。 These are the assemblies added to the .NET console application build folder if I don't embed the interop types for Microsoft.Office.Interop.Excel. 如果我不为Microsoft.Office.Interop.Excel嵌入互操作类型,则这些是添加到.NET控制台应用程序构建文件夹中的程序集。
  • I have .NET for Python installed for 2.7 and 3.5 (from https://pypi.python.org/pypi/pythonnet ) 我已经为2.7和3.5安装了用于Python的.NET(来自https://pypi.python.org/pypi/pythonnet

Thanks for reading. 谢谢阅读。

You can only get to COM objects from pythonnet using reflection currently: 您目前只能使用反射从pythonnet转到COM对象:

clr.AddReference("System.Reflection")
from System.Reflection import BindingFlags

excel.Workbooks.GetType().InvokeMember("Count",
                                       BindingFlags.GetProperty | 
                                       BindingFlags.InvokeMethod, 
                                       None, excel.Workbooks, None)

Here is a wrapper function: 这是一个包装函数:

https://gist.github.com/denfromufa/ec559b5af41060c5ac318f7f59d8b415#file-excel_interop_vsto-ipynb https://gist.github.com/denfromufa/ec559b5af41060c5ac318f7f59d8b415#file-excel_interop_vsto-ipynb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将此代码从Python 2.7转换为Python 3.5以修复 - &gt; AttributeError:&#39;_ io.TextIOWrapper&#39;对象没有属性&#39;next&#39; - How to translate this code from Python 2.7 to Python 3.5 to fix — > AttributeError: '_io.TextIOWrapper' object has no attribute 'next' AttributeError:&#39;guiCreate&#39;对象没有属性&#39;master&#39;-为什么我不能从另一个类访问函数? - AttributeError: 'guiCreate' object has no attribute 'master' - Why can't i access functions from another class? AttributeError:'Queue'对象在python 2.7.x中没有属性'join' - AttributeError: 'Queue' object has no attribute 'join' in python 2.7.x 无法访问python lmdb,&#39;对象没有属性&#39;Environment&#39; - can't get access to python lmdb , ' object has no attribute 'Environment'' Python 2.7&#39;NoneType&#39;对象没有属性 - Python 2.7 'NoneType' object has no attribute Python 2.7“模块”对象没有属性“ setPenRadius” - Python 2.7 'module' object has no attribute 'setPenRadius' win32com Excel.Application 无法再打开文档 - win32com Excel.Application can't open documents anymore 服务器上的django应用程序:我从python 2.7移至3.5,而apache2找不到django - django app on server: I moved from python 2.7 to 3.5 and apache2 can't find django dict()-Python 2.7 3.5以来- - dict () - Python 2.7 From 3.5 - 不能在 Python 2.7 中使用导入的函数 - Can't use imported functions in Python 2.7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM