简体   繁体   English

使用python将文本文件(对象)作为.txt插入到Excel工作表的单元格中

[英]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.

相关问题 我们可以使用 python 解析.txt 文件并将多行解析文本复制到 excel 单元格吗? - Can we parse .txt file and copy multiline parsed text to excel cell using python? 使用 excel 表格数据制作 txt 文件 - Making a txt file using excel sheet data 从 a.txt 文件中读取文本并使用 python 插入字符串 - Read text from a .txt file and insert a string using python 使用 Python 替换文件中 Excel 表中的文本 - Replace the text from an Excel Sheet in a File Using Python Excel 使用 python 和 panda 将数据表转换为文本文件 - Excel Data sheet to a text file using python and panda 使用Python将单元格(X,Y)的值复制到Excel文件的同一张表中的单元格(A,B) - Copying value of cell (X,Y) to cell (A,B) in same sheet of an Excel file using Python Python 将部分 txt 文件提取到 excel 工作表的脚本 - Python Script to extract part of txt file to an excel sheet 如何使用 Python 将 .txt 文件中的数据导入特定的 Excel 工作表? - How to import data from .txt file to a specifc excel sheet with Python? 如何使用 Python 将 Excel 工作表写入制表符分隔的 .txt 文件 - How to write an Excel sheet to a tab separated .txt file with Python 如何使用 python 在 Excel 单元格中显示嵌入的文本文件 - How to display embedded text file in Excel cell using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM