How to export binary image inside text area (created by R data function) to PowerPoint with IronPython

I've got a visualization that is geneated by an R data function and is displayed inside a text area. I would like to have a button with some IronPython code that allows the user to automatically export this graph to PowerPoint.

I can do this manually with File->Expot Data->To Microsoft PowerPoint in the menu. Likewise I can also succesfully export other Spotfire visualization with the following IronPython code below to PowerPoint which I've found from various scourves on the web. However for the graph in the Textare I'm gettin an error message (see error message below). 

I think the issues is caused on line 23 by the  vc.Render(g, r) and I should have probably a Graphis.PromInage()  property defined that works for the binary data type (line 18). Does anyone have some suggestions?

 

 

IronPtycon code that works for Spotfire visualization but not for binary type images rendered in the Text area:

from System.IO import *

from Spotfire.Dxp.Application.Visuals import VisualContent

from System.Drawing import Bitmap, Graphics, Rectangle, Point

import clr

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

    if visual.Title == 'RGraph':

        #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

 

Error with binary image rendered in Text Area:

Traceback (most recent call last):

  File "Spotfire.Dxp.Application", line unknown, in .ctor

  File "Spotfire.Dxp.Application", line unknown, in .ctor

  File "Spotfire.Dxp.Application", line unknown, in .ctor

  File "Spotfire.Dxp.Application", line unknown, in CreateSnapshot

  File "Spotfire.Dxp.Framework", line unknown, in Use

  File "Spotfire.Dxp.Application", line unknown, in <CreateSnapshot>b__16

  File "Spotfire.Dxp.Framework", line unknown, in CreateSnapshot

  File "Spotfire.Dxp.Application.ScriptSupport", line unknown, in ExecuteForDebugging

  File "", line unknown, in <module>

  File "Spotfire.Dxp.Application", line unknown, in Render

  File "Spotfire.Dxp.Application", line unknown, in DrawVisualization

  File "Spotfire.Dxp.Application", line unknown, in DrawVisualizationCore

  File "Spotfire.Dxp.Application", line unknown, in Render

  File "Spotfire.Dxp.Application", line unknown, in CreateView

  File "Spotfire.Dxp.Application", line unknown, in CreateView

StandardError: Exception has been thrown by the target of an invocation.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Attempt take snapshot on application thread in state 'Executing'.

   at Spotfire.Dxp.Framework.Commands.CommandHistory.CreateSnapshot[TNode](TNode node, Executor snapshotSuccededCallback)

   at Spotfire.Dxp.Application.WebView.WebView`1.<>c__DisplayClass18.<CreateSnapshot>b__16()

   at Spotfire.Dxp.Framework.DocumentModel.DocumentView.Use(DocumentView documentView, DocumentViewExecutionState documentViewExecutionState, Action action)

   at Spotfire.Dxp.Application.WebView.WebView`1.CreateSnapshot()

   at Spotfire.Dxp.Application.WebView.WebView`1..ctor(TModel model)

   at Spotfire.Dxp.Application.WebView.VisualWebView`1..ctor(TModel model)

   at Spotfire.Dxp.Application.WebView.Plots.HtmlTextAreaBaseWebView`1..ctor(TModel model)

   --- End of inner exception stack trace ---

   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)

   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

   at Spotfire.Dxp.Application.WebView.WebView`1.CreateView[TWebView](TModel model, UrlGeneratorGenerator imageUrlGeneratorGenerator)

   at Spotfire.Dxp.Application.WebView.WebView`1.CreateView[TWebView](TModel model, UrlGeneratorGenerator imageUrlGeneratorGenerator, Size viewSize)

   at Spotfire.Dxp.Application.WebView.VisualWebView`1.Render[TWebView](TModel model, Graphics graphics, RectangleF layoutRectangle)

   at Spotfire.Dxp.Application.Visuals.HtmlTextArea.DrawVisualizationCore(Graphics graphics, RectangleF layoutRectangle, RenderPurpose renderPurpose)

   at Spotfire.Dxp.Application.Visuals.VisualContent.DrawVisualization(Graphics graphics, RectangleF layoutRectangle, RenderPurpose renderPurpose)

   at Spotfire.Dxp.Application.Visuals.VisualContent.Render(Graphics graphics, Rectangle bounds)

   at _stub_$1306##1306(Closure , CallSite , CodeContext , Object , Object , Object )

   at Microsoft.Scripting.Actions.MatchCaller.Call4[T0,T1,T2,T3,TRet](Func`6 target, CallSite site, Object[] args)

   at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)

   at Microsoft.Scripting.Actions.UpdateDelegates.Update4[T,T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)

   at <module>$1274##1274(Closure , Scope , LanguageContext )

   at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)

   at Spotfire.Dxp.Application.ScriptSupport.IronPythonScriptEngine.ExecuteForDebugging(String scriptCode, Dictionary`2 scope, Stream outputStream)

(2) Answers

Login