我试图在Python中使用OLEObjects在另一个Excel文档中添加Excel文档,但我收到此错误:
d:\Python\PythonReport>d:/Python27/python.exe genDP.py d:/Python/PythonReport/ATP.xlsx
Traceback (most recent call last):
File "genDP.py", line 219, in <module>
add_ATP_WorkBook(OutputBook, sys.argv[1])
File "genDP.py", line 212, in add_ATP_WorkBook
oleObjects.Add(ClassType=None, Filename=file, Link=False, DisplayAsIcon=False, Left=0, Top=0, Width=50, Height=50)
File "<COMObject OLEObjects>", line 6, in Add
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u'Add method of OLEObjects class failed', u'C:\\Program Files (x86)\\Microsoft Office\\Office12\\1033\\XLMAIN11.CHM', 0, -2146827284), None)
奇怪的是,它适用于其他文件:我可以轻松地将文本文档添加为OLEObject,但是当我尝试使用Excel文档时,我会收到该错误。
这是代码:
def add_ATP_WorkBook(OutputBook, file):
ExcelApp = win32com.client.Dispatch("Excel.Application")
ExcelApp.visible = 1
workbook = ExcelApp.Workbooks.open(str(os.path.abspath(os.path.dirname(sys.argv[0])))+"\DVPR Report.xls")
oleObjects = workbook.ActiveSheet.OLEObjects()
oleObjects.Add(ClassType=None, Filename=file, Link=False, DisplayAsIcon=False, Left=0, Top=0, Width=50, Height=50)
workbook.Worksheets('Recognition Rate').Activate()