如何使用python将绘图图保存为Excel工作表作为图像? [英] how to save a plotly graph to excel sheet as an image using python?
本文介绍了如何使用python将绘图图保存为Excel工作表作为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个无花果无花果,现在,我试图将此无花果写为excel文件作为图像.
I created a plotly fig and now, I'm trying to write this fig to excel file as an image.
如何使用Python做到这一点?
How can I do this using Python?
data = []
data.append(
go.Bar(
x=df['id'],
y=df['normalize energy'],
hoverlabel = dict(namelength = -1)
)
)
layout = dict(
title="energy" ,
xaxis=dict(
title='id'
),
yaxis=dict(
title='energy [W]',
titlefont=dict(
color='rgb(148, 103, 189)'
),
tickfont=dict(
color='rgb(148, 103, 189)'
),
overlaying='y',
side='right',
fixedrange=False
),
height= 600
)
fig = go.FigureWidget(data)
fig.layout = layout
fig
writer = pd.ExcelWriter(path)
df.to_excel(writer,'Sheet1')
writer.save()
我也想将无花果添加到excel工作表中.
I want to add the fig to the excel sheet as well.
推荐答案
您可以使用 的组合orca
(用于生成静态图像)和> openpyxl
(用于将图像写入Excel文件).
You could use a combination of orca
(for static image generation) and openpyxl
(for writing images into an excel file).
要安装orca,请参见官方文档.
In order to install orca, see the official doc.
然后,将以下行添加到您的代码中:
Then, append this lines to your code:
fig.write_image("fig.png")
from openpyxl import Workbook
from openpyxl.drawing.image import Image
wb = Workbook()
sheet1 = wb.create_sheet('sheet1',0)
active = wb['sheet1']
active.add_image(Image('fig.png'),'A1')
wb.save('myfile.xlsx')
这篇关于如何使用python将绘图图保存为Excel工作表作为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文