簡體   English   中英

將 plotly figure 轉換為圖像,然后將此圖像用作普通 PNG

[英]convert plotly figure to image and then use this image as normal PNG

我正在嘗試將情節表達的圖形轉換為圖像,然后使用此圖像將其保存在幻燈片上。 這是我的代碼:

import plotly.express as px
import plotly.io as pio
from pptx import Presentation


wide_df = px.data.medals_wide()

fig = px.bar(wide_df, x="nation", y=["gold", "silver", "bronze"], title="Wide-Form Input, relabelled",
             labels={"value": "count", "variable": "medal"})

# Convert the figure to a bytes object
img_bytes = pio.to_image(fig, format='png')

ppt = Presentation(
     "template.pptx"
 )

slide = ppt.slides[3]
placeholder = slide.placeholders[13]

placeholder.insert_picture(
           img_bytes
       )

但我收到以下錯誤消息:

'bytes' object has no attribute 'seek'

你應該能夠做到:

import io

...
...
placeholder.insert_picture(io.BytesIO(img_bytes))

線索是沒有seek屬性的錯誤消息,這是 Python 中“類文件”對象具有的一種方法,而io.BytesIO()是一種使一堆數據看起來來自並表現出來的方法比如,一個文件。 文檔在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM