简体   繁体   中英

How to display embedded text file in Excel cell using python

I have embedded text (document) file into Excel cell, but it's displayed as a big icon , which occupies too much space. How can I change icon into a hyperlink lookalike name fitting the cell?

I have an Excel file text.xlsx . I have embedded a file profile.docx in cell B3 . The following code does that -

import win32com.client as win32
sourceFile = r'E:\test.xlsx'
fileToEmbed = r'E:\profile.docx'
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(sourceFile)
ws = wb.Worksheets("Tabelle1")
dest_cell = ws.Range("B3") #change to your wanted location
obj = ws.OLEObjects()
wordpath = 'C\\Program Files (x86)\\Microsoft Office\\Office 14\\WINWORD.EXE'
obj.Add(ClassType=None,Filename=fileToEmbed,Link=False,DisplayAsIcon=True, IconFileName=wordpath,IconIndex=0,IconLabel='Profile.docx',Left=dest_cell.Left, Top=dest_cell.Top,Width=1,Height=1)
wb.Save()
excel.Application.Quit()

On the left hand side is what I get, and on right hand side is what I want.

在此处输入图像描述

How did you add the file manually on the right picture? It looks like a Hyperlink, not an embedded file. Using embedding manually (through the Excel GUI) I cannot get it to look like that at all. So what you are asking might be impossible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM