Plotly:如何在 Excel 中嵌入完全互动的 Plotly 图形?

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

我正在尝试将交互式情节(或散景)情节嵌入 excel。

为此,我尝试了以下三件事:

  1. 将 Microsoft Web 浏览器用户窗体嵌入 excel,如下:

How do I embed a browser in an Excel VBA form?

这有效并允许在线和离线 html 加载

  1. 创建情节html

'''

import plotly
import plotly.graph_objects as go


x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y = [i**2 for i in x]

fig = go.Figure()

fig.add_trace(go.Scatter(x=x, y=x, mode='markers', name="y=x", marker=dict(color='royalblue', size=8)))
fig.add_trace(go.Scatter(x=x, y=y, name="y=x^2", line=dict(width=3)))

plotly.offline.plot(fig, filename='C:/Users/.../pythonProject/test1.html')
  1. 使用 .Navigate 将 excel 中的 webbrowser 对象重新指向本地 plotly.html。横幅弹出

"...限制此文件显示可以访问您的计算机的活动内容"

点击横幅,我 运行 进入这个错误:

同样HTML可以在浏览器中打开

有什么方法可以在 excel 中显示交互式绘图吗?

交互式绘图需要 javascript 才能工作。 Excel,出于安全原因,阻止 javascript。您可以轻松地将静态图像放入 excel.

在 excel 中包含 javascript 的挑战已在以下问题中解决:How can I use JavaScript within an Excel macro?

如@jerlich 所述,Excel 块 javascript。如果您想要完整的交互性,您应该尝试他们链接的解决方法。

如果您至少想要某种程度的可控性或交互性,请尝试使用 xlwings。使用 excel 按钮,您仍然可以在 Excel 和 Python 之间进行一些通信(包括读取数据和发送图表)。

限制是:

  • 只能使用数据条目和按钮,而不是默认的绘图交互功能。很多都可以复制,但需要更多的工作。
  • 它只能在您可以设置 python 脚本的计算机上运行(但是,看起来 xlwings pro 允许您将程序存储在文件中)
  • 看来你需要通过保存然后添加图形来传递plotly graphs,因为直接传递它们需要xlwings pro。使用 MatPlotLib 可以不用 pro 直接传递。

Plotly guide to making interactive(ish) graphs

xlwings docs on MatPlotLib and Plotly graphs

我喜欢你的问题!我希望我能给你一个更好的答案,但似乎你可以远程实现类似于交互式绘图的唯一方法是使用 pyxll and follow the steps outlined under Charts and plotting / Plotly 包括这样的绘图函数:

from pyxll import xl_func, plot
import plotly.express as px

@xl_func
def 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)

这将产生以下情节:

唉,这不会像我们都知道和喜欢的那样是一个完全互动的情节,因为它也在同一页上声明:

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

但据我所知,这与您将要实现您在问题中寻求的目标一样接近。如果您不限于 Excel,但不知何故限于 Microsoft 领域,其中一位评论者提到您 可以 在 PowerBI 中释放完全交互式的情节。如果这是一个选项,您应该仔细查看 Is it possible to use R Plotly library in R Script Visual of Power BI?。虽然这种方法使用 R...

经过微软QnA and Web Browser Control & Specifying the IE Version

的讨论,我终于把互动剧情带到了excel

要将 Microsoft 网页插入 excel,您必须在注册表编辑器中更改兼容性标志

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Wow6432Node\Microsoft\Office.0\Common\COM 兼容性{8856F961-340A-11D0-A96B-00C04FD705A2}

更改 双字 0 而不是 400

现在您可以将网络浏览器对象插入到 excel,详细步骤为 here

通过添加使用 X-UA 兼容 HTML 元标记的标记手动编辑从 plotly 生成的 HTML 文件

最初生成的 HTML 来自 plotly 的文件看起来像这样

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

已修改 HTML 具有浏览器兼容性

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

此后,我可以在excel中查看交互式情节,也可以像网络浏览器一样进行交互

使用的宏:

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