Embedding text file into excel using Python

╄→尐↘猪︶ㄣ 提交于 2019-12-11 08:25:20

问题


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

来源:https://stackoverflow.com/questions/27743237/embedding-text-file-into-excel-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!