如何使用 Excel VBA 在 Unix 服务器中运行 python 函数? [英] How to run python function in Unix server using Excel VBA?

查看:25
本文介绍了如何使用 Excel VBA 在 Unix 服务器中运行 python 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Excel VBA/VB 脚本自动执行以下步骤.这是我手动执行的操作列表

I'd like to automate the below steps using Excel VBA/VB Script. Here is the list of actions I do manually

第一步:登录winscp.

Step1: login to winscp.

  • 主机名:example.ash.pwj.com
  • 端口:22
  • 用户名:xxx 密码:yyyy
  • 文件协议:SFTP

使用 Winscp 将名为 'testFile' 的文件从本地机器传输到 Unix 服务器中的目录(该目录有一个python脚本'connector.py')

transfer a file named 'testFile' from local machine to a Directory in Unix server using Winscp (the directory has a python script 'connector.py')

Step2:打开腻子.

Step2: Open putty.

  • 目标主机名:example.ash.pwj.com
  • 端口:22
  • 连接类型:SSH
  • 使用凭据登录.

Step3:调用目录下的python脚本.python connector.py argument1 argument2 argument3

Step3: call python script in the directory. python connector.py argument1 argument2 argument3

示例:python connector.py testFile example2.ash.pwj.com 123

我必须使用 Excel VBA/VBScript 处理 WINSCP 文件传输和在腻子中调用 python 脚本.

I have to handle WINSCP file transfer and calling python script in putty using Excel VBA/VBScript.

这是我的代码:

Set sh = CreateObject("WScript.Shell")

sh.Run "pscp -pw yyyy C:\Users\abc\testFile xxx@example.ash.pwj.com:/home/test/dir", 0, True
sh.Run "plink.exe -pw yyyy xxx@example.ash.pwj.com python /home/test/dir/connector.py testFile example2.ash.pwj.com 514", 0, True

推荐答案

你可以去pscpplink 来自 PuTTY 套件:

Set sh = CreateObject("WScript.Shell")

sh.Run "pscp -pw yyyy c:\testFile xxx@example.com:/some/dir/", 0, True
sh.Run "plink -pw yyyy xxx@example.com python /some/dir/connector.py arg1 arg2 arg3", 0, True

请注意,如果路径包含空格,您需要添加适当的引用.

Note that you need to add proper quoting if paths contain spaces.

如果您的目标路径只是没有文件名的目标目录:指定带有尾随文件名的路径.这样,当目标目录不存在时,您将收到错误消息.否则 pscp 文件 user@example.com:/some/dir 可能会创建一个文件 /some/dir 而不是 /some/dir/file/some中没有子目录dir时.

If your destination path is just the destination directory without the filename: specify the path with a trailing filename. That way you'll get an error when the destination directory doesn't exist. Otherwise pscp file user@example.com:/some/dir might create a file /some/dir instead of /some/dir/file when there is no subdirectory dir in /some.

此外,您需要确保将正确的路径传递给您的 Python 脚本.当您将文件复制到 /home/test/dir/ 时,您应该在调用 Python 脚本之前切换到该目录,或者使用完整路径指定文件名:

Also, you need to make sure that you pass the correct path to your Python script. When you copy the file to /home/test/dir/ you should either change into that directory before calling the Python script, or specify the filename with its full path:

sh.Run "plink.exe -pw yyyy xxx@example.ash.pwj.com python /home/test/dir/connector.py /home/test/dir/testFile example2.ash.pwj.com 514", 0, True

/home/test/dir 运行 Python 脚本不会自动使该目录成为当前工作目录.

Running the Python script from /home/test/dir doesn't automatically make that directory the current working directory.

这篇关于如何使用 Excel VBA 在 Unix 服务器中运行 python 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆