Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Info on saving Plotly graphs as static images or HTML
- # https://plotly.com/python/static-image-export/
- import plotly.graph_objects as go
- import numpy as np
- import os
- # Libraries to create PowerPoint files
- from pptx import Presentation
- from pptx.util import Inches
- from PIL import Image
- np.random.seed(1)
- N = 100
- x = np.random.rand(N)
- y = np.random.rand(N)
- colors = np.random.rand(N)
- sz = np.random.rand(N) * 30
- # Store image data in fig
- fig = go.Figure()
- fig.add_trace(go.Scatter(
- x=x,
- y=y,
- mode="markers",
- marker=go.scatter.Marker(
- size=sz,
- color=colors,
- opacity=0.6,
- colorscale="Viridis"
- )
- ))
- if not os.path.exists("images"):
- os.mkdir("images")
- # Kaleido engine is being used (as it's been installed, otherwise Orca engine is used to generate static images)
- # Orca is a pain to configure so use Kaleido!
- # Save image in Raster format
- fig.write_image("images/fig1.webp")
- fig.write_image("images/fig1.jpeg")
- fig.write_image("images/fig1.png")
- # Save image in Vector format
- fig.write_image("images/fig1.svg")
- fig.write_image("images/fig1.pdf")
- # Save the Plotly interactive HTML version to disk
- fig.write_html("images/file.html")
- # Comment out to prevent the interactive HTML version from being launched
- fig.show()
- # SAVING IMAGE TO A POWERPOINT FILE
- # The following snippet ensures that the image is cleanly centered on the PowerPoint slide
- # Code found here:
- # https://pythonprogramming.altervista.org/inserting-an-image-in-powerpoint-with-python/?doing_wp_cron=1608539534.2393460273742675781250
- def _add_image(slide, placeholder_id, image_url):
- placeholder = slide.placeholders[placeholder_id]
- # Calculate the image size of the image
- im = Image.open(image_url)
- width, height = im.size
- # Make sure the placeholder doesn't zoom in
- placeholder.height = height
- placeholder.width = width
- # Insert the picture
- placeholder = placeholder.insert_picture(image_url)
- # Calculate ratios and compare
- image_ratio = width / height
- placeholder_ratio = placeholder.width / placeholder.height
- ratio_difference = placeholder_ratio - image_ratio
- # Placeholder width too wide:
- if ratio_difference > 0:
- difference_on_each_side = ratio_difference / 2
- placeholder.crop_left = -difference_on_each_side
- placeholder.crop_right = -difference_on_each_side
- # Placeholder height too high
- else:
- difference_on_each_side = -ratio_difference / 2
- placeholder.crop_bottom = -difference_on_each_side
- placeholder.crop_top = -difference_on_each_side
- # The following uses the PPTX library to save the image into a PowerPoint Slide
- prs = Presentation()
- layout8 = prs.slide_layouts[8]
- slide = prs.slides.add_slide(layout8)
- title = slide.shapes.title.text = "This is Powerpoint"
- sub = slide.placeholders[2].text = "Python has the power"
- _add_image(slide, 1, "images/fig1.png")
- prs.save("images/MyPresentation.pptx")
Advertisement
Add Comment
Please, Sign In to add comment