gpt4 book ai didi

Add Image to Excel file after pandas.to_excel()(在Pandas.to_EXCEL()之后将图像添加到Excel文件)

转载 作者:bug小助手 更新时间:2023-10-24 19:01:05 26 4
gpt4 key购买 nike



I am using xlsxwriter as the engine that I pass to pandas.ExcelWriter. I have a DataFrame and I call to_excel() on it passing the writer that I've previously acquired. I then try grabbing the worksheet using get_worksheet_by_name(), which seems to work well, and then calling worksheet.insert_image() to insert an image.

我使用xlsxWriter作为引擎传递给pandas.ExcelWriter。我有一个DataFrame,并在其上调用to_exel(),传递我之前获得的编写器。然后,我尝试使用GET_WORKSHEET_BY_NAME()抓取工作表,它似乎工作得很好,然后调用worksheet.Insert_Image()来插入图像。


This isn't working. I have the supposition that to_excel() causes the worksheet to be written and thus not available for mutation later on. Is this correct? If so, would I have to have a manual process for reading every cell in the DataFrame and writing it to Excel myself using xlsxwriter so that I can do the image stuff too? Or is there a way to tell to_excel() not to finish writing the sheet?

这不管用。我假设TO_EXCEL()会导致工作表被写入,因此不能在以后进行更改。这样对吗?如果是这样,我是否必须手动读取DataFrame中的每个单元格,并使用xlsxWriter亲自将其写入Excel,以便我也可以执行图像操作?或者,有没有办法告诉TO_EXCEL()不要完成工作表的编写?


更多回答

to_excel() doesn't write the sheet and lock it. Indeed you can use to_excel() multiple times to write multiple dataframes to the same sheet (just need to change the 'startrow' so they don't write over each other). As you have, executing Xlsxwriter worksheet commands should also work.

TO_EXCEL()不写入工作表并锁定它。实际上,您可以多次使用to_EXCEL()将多个数据帧写入同一张工作表(只需更改‘startrow’,这样它们就不会相互覆盖)。正如您所做的,执行XlsxWriter工作表命令也应该起作用。

优秀答案推荐

This code example will insert a dataframe and the image

此代码示例将插入一个数据帧和图像


The worksheet assignment is just;

worksheet = writer.sheets['Sheet1']

工作表分配为JUST;WORKSHEET=Writer.Sheets[‘Sheet1’]


import pandas as pd

data_frame = pd.DataFrame({'Fruits': ['Appple', 'Banana', 'Mango',
'Dragon Fruit', 'Musk melon', 'grapes'],
'Sales in kg': [20, 30, 15, 10, 50, 40]})

with pd.ExcelWriter("foo.xlsx") as writer:
data_frame.to_excel(writer,
sheet_name="Sheet1",
index=False)

writer.sheets['Sheet1'].insert_image("D2", "pic.png")

However if you were to use

但是,如果您要使用


workbook = writer.book

worksheet = workbook.get_worksheet_by_name('Sheet1')
worksheet.insert_image("D2", "pic.png")

instead of writer.sheets, that would work as well.

而不是Writer.Sheets,这也是可行的。


Example screenshot


更多回答

The problem that I had was related to using column to export specific columns. The calculation for getting the correct column to work with was off. I had to adjust my calculation to work with the actually exported columns. Your sample was very helpful in validating my approach. I even used set_row_pixels() and set_column_pixels() to better place the image.

我遇到的问题与使用Column导出特定列有关。获得要使用的正确列的计算是错误的。我必须调整我的计算以处理实际导出的柱。您的样品对验证我的方法非常有帮助。我甚至使用了set_row_Pixels()和set_Column_Pixels()来更好地放置图像。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com