Export Spotfire to Powerpoint in Python

91 Views Asked by At

I've found a ironpython script that supposedly works and exports Spotfire visualizations to a Powerpoint presentation:

import clr
import spotfire
clr.AddReference("System.IO")
clr.AddReference("Spotfire.Dxp.Application.Visuals")
from System.IO import *
from Spotfire.Dxp.Application.Visuals import VisualContent
from System.Drawing import Bitmap, Graphics, Rectangle, Point
clr.AddReference("Microsoft.Office.Interop.PowerPoint")
import Microsoft.Office.Interop.PowerPoint as PowerPoint

powerpoint = PowerPoint.ApplicationClass()
powerpoint.Visible = True
pres=powerpoint.Presentations.Add()
slideCounter = 1

for visual in Document.ActivePageReference.Visuals:
    #print visual.Title

    #export graphic to temp file
    vc = visual.As[VisualContent]()
    bm = Bitmap(2000, 1200)
    g = Graphics.FromImage(bm)
    r = Rectangle(Point(0,0), bm.Size)
    vc.Render(g, r)
    file = Path.GetTempFileName()
    bm.Save(file)

    #pp setup
    slide=pres.Slides.Add(slideCounter, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)
    slideCounter = slideCounter+1
    slide.Shapes.AddPicture((file), False, True, 30, 60, 650, 400)
    title=slide.Shapes.Title
    txt=slide.Shapes.AddTextBox(1,10,500,500,100)
    title.Top=0.1
    obj=slide.Shapes.Title.TextFrame.TextRange
    obj.Font.Size=24

When I try to run it, I get the error:

File "c:\Users\matti\Desktop\Spotifre\Spotfire\Untitled-2.py", line 4, in <module>
    clr.AddReference("Spotfire.Dxp.Application.Visuals")
System.IO.FileNotFoundException: Could not load file or assembly 'Spotfire.Dxp.Application.Visuals' o una delle relative dipendenze. The system cannot find the file specified.
Nome file: 'Spotfire.Dxp.Application.Visuals'
   in System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   in System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   in System.Reflection.Assembly.Load(AssemblyName assemblyRef)
   in Python.Runtime.AssemblyManager.LoadAssembly(AssemblyName name)
   in Python.Runtime.CLRModule.AddReference(String name)

I really don't understand why I'm getting this error. Could it possibly be that I'm running the code on visual studio code on normal Python and not ironpython? If so, any recommendations on how to set up/download ironpython enviroment?

1

There are 1 best solutions below

2
Gaia Paolini On

I tried running it (as IronPython, as it clearly is using the Spotfire API).

Changing the first few lines to these:

import clr
#import Spotfire
clr.AddReference("System.IO")
#clr.AddReference("Spotfire.Dxp.Application.Visuals")

seems to work for me.

import spotfire threw an error, so I capitalised Spotfire. Then I tried commenting it out and it worked anyway.

Same with the fourth line. Not sure of the purpose, but it threw an error. When I commented it out, the powerpoint file was generated and it contained my visuals, one per page.