2016-07-25 2 views
1

Ich versuche Microsoft.Office.Interop.Excel aus einem Python-Skript zu verwenden:Kein Zugriff auf Funktionen in Excel.Application() Workbooks von Python 2.7/3.5:. '__ComObject' Objekt hat kein Attribut X

Hier
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() 

ist die Ausgabe:

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' 
  • ich in einem Admin bin auf Windows Eingabeaufforderung cmd 10
  • ich versucht habe python 2.7 und 3.5 (mit py -3 exceltest.py).
  • Microsoft.Office.Interop.Excel ist Version 15.0.4420.1017 (Office 2013)
  • Ich erstellte eine ähnliche .NET-Konsole App, die gut funktioniert.
  • Ich habe ILDASM verwendet, um Referenzen von Microsoft.Office.Interop.Excel zu überprüfen, und dann gacutil -l, um zu überprüfen, ob alle referenzierten Assemblys im GAC sind.
  • Nur für den Fall, kopierte ich office.dll, stdole.dll und Microsoft.Vbe.Interop.dll in den Ordner, in dem das Python-Skript ausgeführt wird. Dies sind die Assemblys, die dem Build-Ordner für die .NET-Konsolenanwendung hinzugefügt wurden, wenn ich die Interop-Typen für Microsoft.Office.Interop.Excel nicht einbettte.
  • Ich habe .NET für Python für 2.7 installiert und 3.5 (von https://pypi.python.org/pypi/pythonnet)

Danke fürs Lesen.

Antwort

Verwandte Themen