Plotly: How to embed a fully interactive Plotly figure in Excel? Plotly: How to embed a fully interactive Plotly figure in Excel? vba vba

Plotly: How to embed a fully interactive Plotly figure in Excel?


I like your question! And I'd wish I could give you a better answer, but It seems that the only way you can achieve anything remotely resembling an interactive plotly plot would be to use pyxll and follow the steps outlined under Charts and plotting / Plotly including a plot function like this:

from pyxll import xl_func, plotimport plotly.express as px@xl_funcdef plotly_plot():    # Get some sample data from plotly.express    df = px.data.gapminder()    # Create a scatter plot figure    fig = px.scatter(df.query("year==2007"),                     x="gdpPercap", y="lifeExp",                     size="pop", color="continent",                     log_x=True, size_max=60)    # Show the figure in Excel using pyxll.plot    plot(fig)

This will produce the following plot:

enter image description here

Alas, this will not be a fully interactive plotly plot like we all know and love, since it's also stated on the very same page that:

The plot that you see in Excel is exported as an image so anyinteractive elements will not be available. To make a semi-interactiveplot you can add arguments to your function to control how the plot isdone and when those arguments are changed the plot will be redrawn.

But as far as I know this is as close as you'll get to achieving what you're seeking in your question. If you're not limited to Excel, but somehow limited to the realm of Microsoft, one of the commenters mentioned that you can unleash a fully interactive plotly plot in PowerBI. If that is an option, you should take a closer look at Is it possible to use R Plotly library in R Script Visual of Power BI?. This approach uses R though...


Finally, I have managed to bring the interactive plot to excel after a discussion from Microsoft QnA and Web Browser Control & Specifying the IE Version

To insert a Microsoft webpage to excel you have to change the compatibility Flag in the registry editor

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Wow6432Node\Microsoft\Office\16.0\Common\COM Compatibility{8856F961-340A-11D0-A96B-00C04FD705A2}

Change the DWord 0 instead of 400

Now you can insert the web browser object to excel, Step by step details are here

Edit the HTML File generated from plotly manually by adding a tag for Using the X-UA-Compatible HTML Meta Tag

Originally generated HTML file from plotly looks like this

<html><head><meta charset="utf-8" /></head><body>

Modified HTML with browser compatibility

<html><head>    <meta charset="utf-8" />    <meta http-equiv="X-UA-Compatible" content="IE=edge" /></head><body>

After this, I can able to view the interactive plot in excel also able to do the interactions same as a web browser

Output

Macro used:

Sub Button4_Click()ActiveSheet.WebBrowser1.Navigate "file:///C:/Users/vignesh.rajendran/Desktop/test5.html"End Sub


Interactive plots require javascript to work. Excel, for security reasons, blocks that javascript. You can put a static image easily into excel.

The challenge of including javascript in excel has been addressed in this question: How can I use JavaScript within an Excel macro?