Guest User

Untitled

a guest
Dec 21st, 2020
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. # Info on saving Plotly graphs as static images or HTML
  2. # https://plotly.com/python/static-image-export/
  3.  
  4. import plotly.graph_objects as go
  5. import numpy as np
  6. import os
  7.  
  8. # Libraries to create PowerPoint files
  9. from pptx import Presentation
  10. from pptx.util import Inches
  11. from PIL import Image
  12.  
  13. np.random.seed(1)
  14.  
  15. N = 100
  16. x = np.random.rand(N)
  17. y = np.random.rand(N)
  18. colors = np.random.rand(N)
  19. sz = np.random.rand(N) * 30
  20.  
  21. # Store image data in fig
  22. fig = go.Figure()
  23. fig.add_trace(go.Scatter(
  24.     x=x,
  25.     y=y,
  26.     mode="markers",
  27.     marker=go.scatter.Marker(
  28.         size=sz,
  29.         color=colors,
  30.         opacity=0.6,
  31.         colorscale="Viridis"
  32.     )
  33. ))
  34.  
  35. if not os.path.exists("images"):
  36.     os.mkdir("images")
  37.  
  38. # Kaleido engine is being used (as it's been installed, otherwise Orca engine is used to generate static images)
  39. # Orca is a pain to configure so use Kaleido!
  40. # Save image in Raster format
  41. fig.write_image("images/fig1.webp")
  42. fig.write_image("images/fig1.jpeg")
  43. fig.write_image("images/fig1.png")
  44.  
  45. # Save image in Vector format
  46. fig.write_image("images/fig1.svg")
  47. fig.write_image("images/fig1.pdf")
  48.  
  49. # Save the Plotly interactive HTML version to disk
  50. fig.write_html("images/file.html")
  51.  
  52. # Comment out to prevent the interactive HTML version from being launched
  53. fig.show()
  54.  
  55.  
  56. # SAVING IMAGE TO A POWERPOINT FILE
  57. # The following snippet ensures that the image is cleanly centered on the PowerPoint slide
  58. # Code found here:
  59. # https://pythonprogramming.altervista.org/inserting-an-image-in-powerpoint-with-python/?doing_wp_cron=1608539534.2393460273742675781250
  60. def _add_image(slide, placeholder_id, image_url):
  61.     placeholder = slide.placeholders[placeholder_id]
  62.  
  63.     # Calculate the image size of the image
  64.     im = Image.open(image_url)
  65.     width, height = im.size
  66.  
  67.     # Make sure the placeholder doesn't zoom in
  68.     placeholder.height = height
  69.     placeholder.width = width
  70.  
  71.     # Insert the picture
  72.     placeholder = placeholder.insert_picture(image_url)
  73.  
  74.     # Calculate ratios and compare
  75.     image_ratio = width / height
  76.     placeholder_ratio = placeholder.width / placeholder.height
  77.     ratio_difference = placeholder_ratio - image_ratio
  78.  
  79.     # Placeholder width too wide:
  80.     if ratio_difference > 0:
  81.         difference_on_each_side = ratio_difference / 2
  82.         placeholder.crop_left = -difference_on_each_side
  83.         placeholder.crop_right = -difference_on_each_side
  84.     # Placeholder height too high
  85.     else:
  86.         difference_on_each_side = -ratio_difference / 2
  87.         placeholder.crop_bottom = -difference_on_each_side
  88.         placeholder.crop_top = -difference_on_each_side
  89.  
  90. # The following uses the PPTX library to save the image into a PowerPoint Slide
  91. prs = Presentation()
  92.  
  93. layout8 = prs.slide_layouts[8]
  94. slide = prs.slides.add_slide(layout8)
  95.  
  96. title = slide.shapes.title.text = "This is Powerpoint"
  97. sub = slide.placeholders[2].text = "Python has the power"
  98. _add_image(slide, 1, "images/fig1.png")
  99.  
  100. prs.save("images/MyPresentation.pptx")
Advertisement
Add Comment
Please, Sign In to add comment