python - 如何使用 python 将 plotly graph 保存为 excel 工作表图像?

标签 python excel pandas image plotly

我创建了一个 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/

相关文章:

python - 如何在 Python 中停止 os.system()?

python - 调用 Python 文件后 Excel CSV 输出出现错误

python - 有人可以给我一个关于 WSGI 幕后细节与 Python 的其他 Web 界面方法的高级技术概述吗?

python - 如何将 Format 对象应用于 pandas 中的索引值

python - 在数据帧中的好坏堆叠条上显示前 3 个 'bad' 系统(按一个值排序,但在图表上显示两个值)

python - 在 pytorch 中制作自定义非平凡损失函数

vba - Excel VBA ClearContents 错误

c++ - 在 Excel XLL 中返回数组

excel - 做直到循环。满足一个或另一个条件

python - 用 Pandas 折叠重复的行