Jump to content
  • Exporting Spotfire visualization(s) inside a Microsoft PowerPoint template


    This article described a method to export a visualization and related objects (title, description) to specific places in a Microsoft PowerPoint template document.


    Introduction

    Analysts may spend significant amounts of time visualizing data in Spotfire and conveying insights in presentation slides to succinctly and accurately explain findings.

    Spotfire allows easy exporting of analysis to PowerPoint presentations and PDF documents, or exporting and scheduling prepared reports to PDF, or exporting a selected visualization as PDF or image. But Spotfire does not allow the inserting of a selected visualization into a specific location inside a PowerPoint template.

    Objective

    Describe a method to export a visualization and related objects (title, description) to specific places in a PowerPoint template document in order to limit time spent on manually building PowerPoint slides out of Spotfire visualizations.

    Pre-requisites

    Software requirements

    • Spotfire 10.10 LTS or later
    • Microsoft PowerPoint 2007 or later

    Install python-pptx library

    python-pptx is a Python library for creating and updating Microsoft PowerPoint (.pptx) files by adding slides, images, graphs, shapes, titles, texts and more.

    Local installation

    The python-pptx library is not available from Spotfire Python tools which means you'll have to install the library manually using command lines.

    In Spotfire desktop client, open Python tools (Tools > Python Tools) and Copy Python Interpreter Path to Clipboard.

    User-added image

    Then open a Terminal as administrator and type the following:
    cd {paste Python interpreter path}
    .python.exe -m pip install python-pptx

    Once finished, you can verify the library installed successfully from the Spotfire Python tools (Tools > Python Tools > Package Management. python-pptx should appear on the list of installed packages.

    User-added image


    Governed installation

    While it is convenient for individual users to add libraries, managers and administrators may be concerned about which libraries are used in their organization and may want to standardize usage.

    Spotfire makes it easy for administrators and superusers to control libraries by deploying them to the relevant deployment areas of the Spotfire Server. Learn how to deploy packages from the Spotfire documentation.


    Prepare the template PowerPoint document


    Delete all slides

    Open your company presentation template (.pptx) in Microsoft PowerPoint and delete all slides in the document as any slides in the file will be automatically added to a new slide deck. The python-pptx library does not allow deletion of slides, so we need the template file to be empty.


    Generate placeholders markups

    To choose exactly where to add Spotfire objects in a template, we need to generate a labeled markup of slide layouts and placeholder types from the PowerPoint template document. We need to run a script in the Terminal with the empty PowerPoint template document as input to generate markups.

    From a web browser, download the analyze_ppt.py script.

    Back in the Terminal, type the following:
    .python.exe {path to analyze_ppt.py script} {path to the .pptx template} {path to where to save markup .pptx}

    Reopening the PowerPoint template document will now show markups.

    User-added image

    Spotfire visualization to PowerPoint

    For this tutorial we will use the Sales & Marketing sample analysis available in the Spotfire library.

    User-added image

    Let?s say we want to export the Performance Matrix visualization into the PowerPoint template with the following objects:

    • Title
    • Description
    • Visualization


    Creating a script to export visualization to image

    We will now add an IronPython script to enable exporting the Performance Matrix visualization to an image saved locally with a simple click on a button.

    Let?s create a Text area, and edit it to add an Action control.
    On the Action control dialog, select Script, and New? Give the script a name and under script parameters add the Performance Matrix visualization. We name this parameter ?visual?.

    We can now write the script for exporting the visualization to image as follows:

    from Spotfire.Dxp.Application.Visuals import *
    from System.Drawing import Size
    from System.Threading import CancellationToken
    from Spotfire.Dxp.Framework.ApplicationModel import ApplicationThread
    
    app=Application.GetService(ApplicationThread)
    size=Size(800,600)
    rrs=RenderResultSettings(size)
    vrs=VisualRenderSettings()
    vrs.ShowAnnotations=Visibility.Hidden
    vrs.ShowLegend=Visibility.Hidden
    vrs.ShowTitle=Visibility.Hidden
    ct=CancellationToken()
    
    def export(app=app, visual=visual, rrs=rrs, vrs=vrs, ct=ct):
            try:
                    rresult=visual.RenderAsync(rrs,vrs,ct)
            except:
                    return
            img=rresult.Result.AsImage()
            img.Save("C:{path to where to save image}\\"+visual.Title+".png")
    
    app.InvokeAsynchronously(export)
     

    Now we can test if the export to image is working. Click Run Script.
    You will find the visualization export image in the folder you chose for export. The export will look like this:

    User-added image

    Click OK and give a name for the Action control button.
    We now have a button in analysis to export the Performance Matrix visualization to a local image.

    User-added image

    Now let's look at how to insert this image into a PowerPoint.


    Inserting a visualization image to PowerPoint

    We will now create a Python data function that will use the python-pptx library to create a new PowerPoint document and insert the visualization in a chosen slide layout in a specific picture placeholder.


    Save visualization export path as a document property

    We will need to indicate the path of the exported visualization image to our future data function. Let?s update our script to do that automatically.

    Go to File > Document Properties > Properties and add a new property of type String. We named it ?visualization.path?. Leave the value empty and click OK, and OK again.

    Now back in the Text area, edit it and double click the Action control button to edit the script. Click Edit?
    From there we just need to add a single line just after img.Save to populate the document property we just created, as follows:

     Document.Properties["visualization.path"]="C:{path to where the image is saved}\"+visual.Title+".png"

    Create the data function

    To create the data function, go to Data > Data Function Properties and click Register New?

    Make sure Python is selected as Type, give the function a name and eventually a description and icon. For instance, in this example:

    • Name: ?Insert visualization in slide layout 8?
    • Description: ?This function inserts a given image into slide layout 8 in our corporate Microsoft PowerPoint slides template.?

    We can then write the script for inserting the visualization image in the PowerPoint document as follows:

    from pptx import Presentation
    
    def add_image(slide, placeholder_id, image_url):
        placeholder = slide.placeholders[placeholder_id]
        placeholder = placeholder.insert_picture(image_url)
        return(slide)
    
    prs = Presentation(pptx_template)
    slide_layout = prs.slide_layouts[layoutid]
    slide = prs.slides.add_slide(slide_layout)
    slide = add_image(slide,picture_placeholderid,visualization_path)
    
    prs.save(pptx_save)
    pptx_path = pptx_save
    
     

    Now we can configure input parameters as follows:

    • pptx_template (type: Value, allowed: String): Will set the location of the PowerPoint template markup we created previously.
    • layoutid (type: Value, allowed: Integrer): Will set the slide layout style chosen from the PowerPoint template.
    • picture_placeholderid (type: Value, allowed: Integer): Will set the location in the slide layout where to insert the image.
    • visualization_path (type: Value, allowed: String): Will set the location of the exported image to be included into the PowerPoint document.
    • pptx_save (type: Value, allowed: String): Will set the location (path) to the new PowerPoint document where to save the created slide that includes the visualization.

    User-added image

    You should try to name and describe each input parameter as clearly as possible as they will be reflected in the final end-user interface of the data function.

    And then set the output parameter as follow:

    • pptx_path (type: Value, allowed: String): Will set the location (path) to the new PowerPoint document as a document property (will not be used).

    The data function is now ready. We can save it to the Spotfire Library (Save As) and close all dialogs.


    Add data function to analysis

    We can now add the data function we just created in our Spotfire analysis. Open the F(x) flyout panel and on the search bar click the ellipsis button and then Find items in the library...

    User-added image


    and search for the data function we just created and launch it.

    User-added image


    We can now configure the data function input parameters as follows:

    • Microsoft PowerPoint Template: Path to the PowerPoint template markup we created previously
    • Slide Layout ID: Slide layout style chosen from the PowerPoint template
    • Picture Placeholder ID: Location in the slide layout where to insert the image
    • Microsoft PowerPoint: Path to the new PowerPoint document where to save the created slide that includes the visualization.

    Make sure to keep "Refresh function automatically" disabled and click OK.

    User-added image

    Now the data function asks about the output value which is the path to the new PowerPoint document where we added the slide. We will add this as a document property and click OK.


    Automating visualization export and insertion into the PowerPoint document

    This last step is about running the data function automatically right after the visualization is exported to an image. To do that we will just update the script attached to the Action control button.

    Back in the Text area, edit it and double click the Action control button to edit the script. Click Edit? from there we will add this additional step to run the data function at the end of the script, as follows:

    In the header, add:

     from Spotfire.Dxp.Data.DataFunctions import DataFunctionExecutorService, DataFunctionInvocation, DataFunctionInvocationBuilder
     

    Then at the end of the script add:

    dataFunction = None
    for function in Document.Data.DataFunctions:
        if function.Name == '{Your data function name}':
            dataFunction = function
            dataFunction.Execute()
     

    The final script should look something like this:

    from Spotfire.Dxp.Application.Visuals import *
    from System.Drawing import Size
    from System.Threading import CancellationToken
    from Spotfire.Dxp.Framework.ApplicationModel import ApplicationThread
    from Spotfire.Dxp.Data.DataFunctions import DataFunctionExecutorService, DataFunctionInvocation, DataFunctionInvocationBuilder
    
    app=Application.GetService(ApplicationThread)
    size=Size(800,600)
    rrs=RenderResultSettings(size)
    vrs=VisualRenderSettings()
    vrs.ShowAnnotations=Visibility.Hidden
    vrs.ShowLegend=Visibility.Hidden
    vrs.ShowTitle=Visibility.Hidden
    ct=CancellationToken()
    
    def export(app=app, visual=visual, rrs=rrs, vrs=vrs, ct=ct):
            try:
                    rresult=visual.RenderAsync(rrs,vrs,ct)
            except:
                    return
            img=rresult.Result.AsImage()
            img.Save("C:{path to where to save image}\\"+visual.Title+".png")
            Document.Properties["visualization.path"]="C:{path to where the image is saved}\\"+visual.Title+".png"
    app.InvokeAsynchronously(export)
    
    dataFunction = None
    for function in Document.Data.DataFunctions: 
            if function.Name == '{Your data function name}': 
                    dataFunction = function 
                    dataFunction.Execute()
     

    And you are done. Clicking the button now exports the visualization to image and inserts in a PowerPoint presentation document. Here is the result:

    User-added image

    Adding more than just a visualization

    This article demonstrated how to insert one Spotfire visualization in a PowerPoint presentation using scripting. This can be extended to more advanced usage leveraging the full capabilities of the python-pptx library. Not only visualization can be added to PowerPoint slides but also titles, texts and even graphs generated from data in Spotfire. Here are just a few additional examples?


    Titles and text

    Title and text placeholders may be filled with content from Spotfire either by using property controls to set them manually from a Spotfire report or from titles and descriptions from visualizations. It is just a matter of setting document properties that will be reused by the data function for injection in the PowerPoint slide.


    Example: Inserting visualization title and description

    Update IronPython script

    Add title and description as document properties:

     Document.Properties["{viz.title}"] = visual.Title
     Document.Properties["{viz.description}"] = visual.Description
     
    Update Data Function

    Add input parameters to insert these in appropriate placeholders:

     title = slide.shapes.title
     title.text = viz.title
     notes = slide.placeholders[notes_placeholderid]
     notes.text = viz.description

    User-added image


    End-user selection of a visualization

    Slides may contain multiple picture placeholders and you may want to insert multiple visualizations at once from Spotfire. For that, we need a way to select multiple visualizations in Spotfire to send out to the script.


    Example: Allow selection of one or multiple visualizations

    Update Text area

    Add a property control of type List box or Dropdown for single selection or List box (multiselect) for multiple selection. From the dialog, add a new property called ?visualization.selection? of type string. Then set the property value through Fixed values and enter the visualizations names for the visualization you want to enable for export.

    Update IronPython script

    Change script parameter to the page (vs specific visualization) and loop through the selection of visualizations to export:

    for visual in page.Visuals:
            if str(visual.Title) == Document.Properties["visualization.selection"]:
                    app=Application.GetService(ApplicationThread)
                    size=Size(800,600)
                    rrs=RenderResultSettings(size)
                    …
     
    Update Data Function

    Update the Python script and input parameters to insert additional picture placeholders.

    from pptx import Presentation
    
    def add_image(slide, placeholder_id, image_url):
        placeholder = slide.placeholders[placeholder_id]
        placeholder = placeholder.insert_picture(image_url)
        return(slide)
    
    def add_image2(slide, placeholder_id, image_url):
        placeholder = slide.placeholders[placeholder_id]
        placeholder = placeholder.insert_picture(image_url)
        return(slide)
    
    def add_image3(slide, placeholder_id, image_url):
        placeholder = slide.placeholders[placeholder_id]
        placeholder = placeholder.insert_picture(image_url)
        return(slide)
    
    prs = Presentation(pptx_template)
    slide_layout = prs.slide_layouts[layoutid]
    slide = prs.slides.add_slide(slide_layout)
    slide = add_image(slide,picture_placeholderid,visualization_path)
    slide = add_image2(slide,picture_placeholderid2,visualization_path2)
    slide = add_image3(slide,picture_placeholderid3,visualization_path3)
    
    prs.save(pptx_save)
    
     

    These are just simple examples of what you can achieve using the python-pptx library as Spotfire data functions. You can build data functions associated with each PowerPoint slide template layout and make them reusable as tools inside Spotfire or automatized and invisible using scripting like described in this article.
     

    More about the python-pptx library

    This library is available with an MIT license.
    Full API documentation is available here.

     

     


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...