[英]Insert a text file (object) as .txt into a cell of excel sheet using python
I want to embed a text file in to excel sheet using python (as shown above)such that when you click on that cell, the file automatically opens. 我想使用python将文本文件嵌入到Excel工作表中(如上所示),这样当您单击该单元格时,该文件会自动打开。 I do not want to specify the path as hyperlink.like "c:\\My Documents\\xyz.txt".
我不想将路径指定为超链接。如“c:\\ My Documents \\ xyz.txt”。 The reason for this is that I want the code to be portable , ie, others should also be able to run the program on their computers in whichever folder they want to.
这样做的原因是我希望代码是可移植的,即其他人也应该能够在他们想要的任何文件夹中的计算机上运行程序。 In this situation the code wont work on other computers due to the path name specified.
在这种情况下,由于指定了路径名,代码将无法在其他计算机上运行。
import win32com.client as win32
xl = win32.Dispatch('Excel.Application')
xl.Visible = 1
wb = xl.Workbooks.Open("C:\Users\\automation\\abcdef.xlsx")
column = wb.ActiveSheet.Range("A2:A200")
Embedded_object = wb.ActiveSheet.OLEObjects()
i = 2
for cell in column:
hostname_cell = wb.ActiveSheet.Cells(i,1).Value
fi = 'C:\Users\\212632723\utomation\\geckodriver.txt'
if hostname_cell is None:
print fi
xl.ActiveSheet.OLEObjects().Add(Filename=fi, Link=False, DisplayAsIcon=True).Select
i += 1
I have used above code from Embedding text file into excel using Python to accomplish what i needed, but it throws an error 我已经使用上面的代码从使用Python嵌入文本文件到excel来完成我需要的东西,但它会抛出一个错误
xl.ActiveSheet.OLEObjects().Add(Filename=fi, Link=False, DisplayAsIcon=True).Select File "", line 6, in Add com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u'Add method of OLEObjects class failed', u'xlmain11.chm', 0, -2146827284), None)
xl.ActiveSheet.OLEObjects()。Add(Filename = fi,Link = False,DisplayAsIcon = True)。在Add com_error中选择文件“”,第6行:( - 214703567,'Exception occurred。',(0,u' Microsoft Excel',u'Add方法的OLEObjects类失败',u'xlmain11.chm',0,-2146827284),无)
EDIT : 编辑:
import win32com.client as win32
xl = win32.Dispatch('Excel.Application')
xl.Visible = 1
wb = xl.Workbooks.Open("C:\Users\\212632723\python_ui_automation\\abcdef.xlsx")
column = wb.ActiveSheet.Range("A2:A200")
Embedded_object = wb.ActiveSheet.OLEObjects()
i = 2
for cell in column:
hostname_cell = wb.ActiveSheet.Cells(i,1).Value
fi = 'C:\Users\\212632723\python_ui_automation\\geckodriver.txt'
if hostname_cell is None:
print fi
#xl.ActiveSheet.OLEObjects().Add(Filename=fi, Link=False, DisplayAsIcon=True).Select
xl.ActiveSheet.OLEObjects().Add(Filename=fi, Link=False, DisplayAsIcon=True, IconFileName="excel.exe", IconIndex=0, IconLabel="IcoLabel").Select()
i += 1
I have tried this based on the comments and it throws following error 我已经根据评论尝试了这个,它会引发跟随错误
Traceback (most recent call last):
Traceback(最近一次调用最后一次):
File "C:\\Users\\212632723\\python_ui_automation\\hello.py", line 15, in xl.ActiveSheet.OLEObjects().Add(Filename=fi, Link=False, DisplayAsIcon=True, IconFileName="excel.exe", IconIndex=0, IconLabel="IcoLabel").Select() File "", line 6, in Add com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u'Add method of OLEObjects class failed', u'xlmain11.chm', 0, -2146827284), None)
文件“C:\\ Users \\ 212632723 \\ python_ui_automation \\ hello.py”,第15行,在xl.ActiveSheet.OLEObjects()中添加(Filename = fi,Link = False,DisplayAsIcon = True,IconFileName =“excel.exe”, IconIndex = 0,IconLabel =“IcoLabel”)。选择()文件“”,第6行,在添加com_error:( - 214703567,'异常发生。',(0,u'Microsoft Excel',u'Add方法的OLEObjects class failed',u'xlmain11.chm',0,-2146827284),None)
I think that this post: 我认为这篇文章:
Embedding text file into excel using Python 使用Python将文本文件嵌入到Excel中
answers the first part of your question. 回答你问题的第一部分。
Concerning your path issue, you could try to use relative paths or environment variable (on Windows, you can rely on HOMEPATH). 关于路径问题,您可以尝试使用相对路径或环境变量(在Windows上,您可以依赖HOMEPATH)。
Regards. 问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.