Python 将纯文本标题和图像导出到Excel

Python 将纯文本标题和图像导出到Excel,python,excel,pandas,Python,Excel,Pandas,我对Python相当陌生,但在文件的DataFrame.to_excel()部分中,我一直在尝试将图像文件传递到头文件中 基本上,我想要的是Excel表格第一个单元格中的一张图片,后面是几行(准确地说是5行)文本,其中包括一个日期(可能是从datetime.date.today().ctime(),如果可能的话) 我已经有了输出表部分的代码,如下所示: mydataframe.to_excel(my_path_name, sheet_name= my_sheet_name, index=Fals

我对Python相当陌生,但在文件的
DataFrame.to_excel()
部分中,我一直在尝试将图像文件传递到头文件中

基本上,我想要的是Excel表格第一个单元格中的一张图片,后面是几行(准确地说是5行)文本,其中包括一个日期(可能是从
datetime.date.today().ctime()
,如果可能的话)

我已经有了输出表部分的代码,如下所示:

mydataframe.to_excel(my_path_name, sheet_name= my_sheet_name, index=False, startrow=7,startcol=0)
有没有办法直接从Python输出图像和文本部分

更新:


为清楚起见,
mydataframe
正在导出工作表的内容(数据行和数据列)。我已经在Excel工作表的第7行开始了。收割台部分是故障点

我找到了解决方案,感谢所有的帮助

简单的答案是使用
xlsxwriter
包作为引擎。换句话说,假设图像保存在路径
/image.png
处。然后,将图像位于数据顶部的数据插入excel文件的代码为:

# Importing packages and storing string for image file
import pandas as pd
import xlsxwriter
import numpy as np
image_file = '/image.png'

# Creating a fictitious data set since the actual data doesn't matter
dataframe = pd.DataFrame(np.random.rand(5,2),columns=['a','b'])

# Opening the xlsxwriter object to a path on the C:/ drive
writer = pd.ExcelWriter('C:/file.xlsx',engine='xlsxwriter')

dataframe.to_excel(writer,sheet_name = 'Arbitrary', startrow=3)

# Accessing the workbook / worksheet
workbook = writer.book
worksheet = writer.sheets['Arbitrary']

# Inserting the image into the workbook in cell A1
worksheet.insert_image('A1',image_file)

# Closing the workbook and saving the file to the specified path and filename
writer.save()

现在我的excel文件顶部有一个图像。胡萨

你能告诉我们更多关于
mydataframe
中的内容吗?您实际上是在传递图像对象还是仅传递图像的路径?@ASGM我没有将图像保存到
mydataframe
中。我真的只是想看看是否可以这样做,而不是每次都要在Excel中更新它。我确实使用
image.open(file\u ref)
导入了图像,然后您可以使用
工作表将文本导出到同一工作表的不同单元格中。write\u string()
工作表。write\u rich\u string()
功能。