I am fairly new to Python, but I'm getting stuck trying to pass an image file into a header during the
mydataframe.to_excel(my_path_name, sheet_name= my_sheet_name, index=False, startrow=7,startcol=0)
I found the solution and thanks for all of the help.
The simple answer is to use the
xlsxwriter package as the engine. In other words assume that the image is saved at the path
/image.png. Then the code to insert the data into the excel file with the image located at the top of the data would be:
# 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()
And now I have an image on the top of my excel file. Huzzah!