我尝试使用Python将文本文件嵌入到Excel中:
xl = win32.Dispatch('Excel.Application')
xl.Visible = 1
wb = xl.Workbooks.Open("C:\inventory\INVENTORY.xls")
column = wb.ActiveSheet.Range("D2:D200")
i = 2
for cell in column:
hostname_cell = wb.ActiveSheet.Cells(i,1).Value
filename = 'C:\ioe\\' + str(hostname_cell) + '.txt'
if hostname_cell is not None:
print filename
xl.ActiveSheet.OLEObjects().Add(FileName=filename, Link=False, DisplayAsIcon=True).Select
i += 1
但是我收到了这个错误:
TypeError: Add() got an unexpected keyword argument 'FileName'
我在网上搜索过,但无法理解为什么,有什么想法?
编辑:如果我将代码更改为:
,我会收到同样的错误f = 'C:\ioe\\' + str(hostname_cell) + '.txt'
if hostname_cell is not None:
print f
xl.ActiveSheet.OLEObjects().Add(FileName=f, Link=False, DisplayAsIcon=True).Select
i += 1
或
if hostname_cell is not None:
print f
xl.ActiveSheet.OLEObjects().Add(FileName='C:\Users\\robertph\Share\ioe\\' + str(hostname_cell) + '.txt', Link=False, DisplayAsIcon=True).Select
i += 1