Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python将文本文件嵌入excel单元格_Excel_Python 3.x - Fatal编程技术网

如何使用python将文本文件嵌入excel单元格

如何使用python将文本文件嵌入excel单元格,excel,python-3.x,Excel,Python 3.x,我想用python脚本将图像、文本文件嵌入到excel表格中。 然后,我们可以通过单击单元格内的对象来检查文件的内容 使用下面的代码,我能够将图像插入特定的单元格(C2)。 但我关心的是如何将图像作为对象或文本文件插入到工作表的特定单元格中。 请查看屏幕截图以供参考 import openpyxl import time import datetime from openpyxl.drawing.image import Image ################### Todays da

我想用python脚本将图像、文本文件嵌入到excel表格中。 然后,我们可以通过单击单元格内的对象来检查文件的内容

使用下面的代码,我能够将图像插入特定的单元格(C2)。 但我关心的是如何将图像作为对象或文本文件插入到工作表的特定单元格中。
请查看屏幕截图以供参考

import openpyxl 
import time
import datetime
from openpyxl.drawing.image import Image

################### Todays date ###############################
dateToday=datetime.datetime.today()
FormatedDate=('{:02d}'.format(dateToday.day)+'-'+'{:02d}'.format(dateToday.month)+'-'+'{:04d}'.format(dateToday.year))
print (FormatedDate)
# 阅读工作手册 加载工作簿 访问工作簿的第一个工作表 AccessFile=LoadFile.active ##################表1 访问工作簿中的特定工作表。
谢谢,有没有其他开源模块可以做同样的嵌入文件。我需要在没有微软应用程序的Linux机器上做同样的事情。
Sigos_DailyHealthCheckReport = r'D:\Script\Monitoring\CheckReport-6-Dec-2017.xlsx'
LoadFile = openpyxl.load_workbook(Sigos_DailyHealthCheckReport) 
Sheet2 = LoadFile.get_sheet_by_name('Operational Status Of SITE') 

img = Image("D:\Script\Monitoring\Dashboard.png", size=[140,140])
Sheet2['A1'] = 'This is Sid'

Sheet2.add_image(img, 'C2')

LoadFile.save("CheckReport.xlsx")
LoadFile.close()
###### Finally I have develop the script for the above mention question.
###### I am share it so that someone can make use of it if he/she is searching resolution for similar issue.

##pip install pypiwin32 to work with windows operating sysytm and import the module as mentioned below.
import win32com.client 
# Creating an object for accessing excel application.
excel_app = win32com.client.Dispatch('Excel.Application')
# Set visible as 1. It is required to perform the desired task.
excel_app.visible = 1
# Open the excel workbook from the desired location in read mode.
workbook = excel_app.Workbooks.Open(r'D:\Script\Monitoring\DailyHealthCheckReport.xlsx')
# Select worksheet by name.
worksheet = workbook.Sheets('Operational Status Of SITE')
# To assign an object for OLEObject(=EMBED("Packager Shell Object","")). 
Embedded_object = worksheet.OLEObjects()
# To assign loction of the image file that need to inserted as OBJECT in excel worksheet.
file_loction = "D:\Script\Monitoring\Dashboard.png"
# To add selected file to the excel worksheet. It will add the OBJECT to the A1 cell of the current worksheet.
Embedded_object.Add(ClassType=None, Filename=file_loction, Link=False, DisplayAsIcon=True,Left=3, Top=0, Width=50, Height=50)
# To Copy selected range of cells in the current worksheet.
worksheet.Range('A1:A1').Copy()
# To paste the copied data to a perticular range of cells in currnet worksheet.
worksheet.Paste(Destination=worksheet.Range('C2:C2'))
# To select fist item in the list of object i.e. first object.
obj = Embedded_object.Item(1)
# To delete selected object from the worksheet.
obj.Delete()