Embedding text file into excel using Python

2019-07-24 19:02发布

I'm trying to embed a text file into Excel using Python:

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

But I am getting this error:

TypeError: Add() got an unexpected keyword argument 'FileName'

I've searched online but can't understand why, any thoughts?

EDIT:I get the same error if I change the code to:

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

or

    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

0条回答
登录 后发表回答