[英]How to translate this code from Python 2.7 to Python 3.5 to fix — > AttributeError: '_io.TextIOWrapper' object has no attribute 'next'
[英]Can't access functions in Excel.Application().Workbooks from Python 2.7/3.5: '__ComObject' object has no attribute X
我正在嘗試通過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()
這是輸出:
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'
謝謝閱讀。
您目前只能使用反射從pythonnet轉到COM對象:
clr.AddReference("System.Reflection")
from System.Reflection import BindingFlags
excel.Workbooks.GetType().InvokeMember("Count",
BindingFlags.GetProperty |
BindingFlags.InvokeMethod,
None, excel.Workbooks, None)
這是一個包裝函數:
https://gist.github.com/denfromufa/ec559b5af41060c5ac318f7f59d8b415#file-excel_interop_vsto-ipynb
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.