怎样在Excel中使用python脚本,python操作excel详细教程

首页 > 时尚 > 作者:YD1662025-05-13 10:18:34

怎样在Excel中使用python脚本,python操作excel详细教程(1)

在Excel中准备调用的python代码:def merge_sort_unique(lists):

s = set()

for L in lists:

s.update(L)

return sorted(s)任意在本地建立一个目录,并保存这个代码并命名为Methods.py。我们一步步看看如何从Excel的工作表中如何调用这个脚本。打开Excel,填写一些数据以便提供python脚本处理,并保持这个表格于刚才的python脚本在同一目录下。你的工作表应该如下:

接下来,打开VBA并提交给ExcelPython。

打开VBA按 Alt + F11 然后进入工具界面的 References···如果你已经正确安装了ExcelPython你可以从对话框中选择。

现在,备调用python脚本,创建一个新的模块(Insert | Module)并敲以下VBA代码:

Function msu(lists As Range)

Set methods = PyModule("Methods", AddPath:=ThisWorkbook.Path)

Set result = PyCall(methods, "merge_sort_unique", PyTuple(lists.Value2))

msu = WorksheetFunction.Transpose(PyVar(result))

ExitFunction

EndFunction

在Excel中准备调用的python代码:def merge_sort_unique(lists):

s = set()

for L in lists:

s.update(L)

return sorted(s)任意在本地建立一个目录,并保存这个代码并命名为Methods.py。我们一步步看看如何从Excel的工作表中如何调用这个脚本。打开Excel,填写一些数据以便提供python脚本处理,并保持这个表格于刚才的python脚本在同一目录下。你的工作表应该如下:

接下来,打开VBA并提交给ExcelPython。

打开VBA按 Alt + F11 然后进入工具界面的 References···如果你已经正确安装了ExcelPython你可以从对话框中选择。

现在,备调用python脚本,创建一个新的模块(Insert | Module)并敲以下VBA代码:

Function msu(lists As Range)

Set methods = PyModule("Methods", AddPath:=ThisWorkbook.Path)

Set result = PyCall(methods, "merge_sort_unique", PyTuple(lists.Value2))

msu = WorksheetFunction.Transpose(PyVar(result))

ExitFunction

EndFunction

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.