我创建了一个 plotly fig,现在,我正在尝试将这个 fig 作为图像写入 excel 文件。
如何使用 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 表中。
最佳答案
您可以使用 orca
的组合(用于静态图像生成)和 openpyxl
(用于将图像写入 excel 文件)。
要安装 orca,请参阅 official doc .
然后,将这几行附加到您的代码中:
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 - 如何使用 python 将 plotly graph 保存为 excel 工作表图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60914595/