我正在努力将我们在 MS Word for Windows 中使用的宏移植到 OSX。该宏允许用户在 Word 中使用 LaTeX 生成方程式,并且必须向服务器发出 POST 请求并返回结果。这在使用 Microsoft.XMLHTTP 对象的 Windows 中运行良好,但在 OSX 中似乎没有等效项。
为了解决这个问题,我创建了一个 Python 脚本,它使用 urllib 和 urllib2 来处理请求,并允许我发送我的 LaTeX 字符串和网址使用 argparse 输入参数。此脚本执行我需要的操作,并将网络查询的结果作为字符串返回。
我在 VBA 中需要这个字符串。
我当前的 VBA 调用类似于以下内容,其中 pyPath 和 getURLpath 是静态的,Latex_Str 和 Font_Size 由用户输入生成,WebAdd 是运行我们的服务器端脚本的服务器地址。
sCmd = """" & pyPath & "python"" """ & getURLpath & "getURL.py"" --formula """ & _
Latex_Str & """ --fontsize " & Font_Size & " """ & WebAdd & """"
sResult = Shell(sCmd, vbNormalFocus)
问题是 Shell 命令仅返回您正在调用的进程的双值 PID。我需要从我的 Python 脚本返回的实际字符串。我可以修改 Python 脚本以根据需要返回它,但如何将它导入 VBA?
有人知道如何将此结果导入我的 VBA 环境吗?
请您参考如下方法:
从问题中提取的答案:
我成功了!
我能够使用 result = MacScript(command) 函数调用我的 Python 脚本,我在其中定义了我的 command 如下:
command = "do shell script """ & pyPath & "python " & getURLpath & "getURL.py --formula '" _
& Latex_Str & "' --fontsize " & Font_Size & " " & WebAdd & """"
我的 Python 脚本名为 getURL.py,它根据可选参数 --formula 和 --fontsize 处理对服务器的请求code> 由用户定义并在 VBA 中分别存储为 Latex_Str 和 Font_Size,以及服务器的网址 WebAdd。我还在我的 Python 脚本中添加了一些功能来处理传递的代理设置。上面通过 MacScript 传递的命令返回 Python 脚本的标准输出,这是从服务器返回的。 Python脚本如下:
# Import the required libraries
from urllib import urlencode
from urllib2 import Request, urlopen, URLError, ProxyHandler, build_opener, install_opener
import argparse
# Set up our argument parser
parser = argparse.ArgumentParser(description='Sends LaTeX string to web server and returns meta data used by LaTeX in Word project')
parser.add_argument('webAddr', type=str, help='Web address of LaTeX in Word server')
parser.add_argument('--formula', metavar='FRML', type=str, help='A LaTeX formula string')
parser.add_argument('--fontsize', metavar='SIZE', type=int, default=10, help='Integer representing font size (can be 10, 11, or 12. Default 10)')
parser.add_argument('--proxServ', metavar='SERV', type=str, help='Web address of proxy server, i.e. http://proxy.server.com:80')
parser.add_argument('--proxType', metavar='TYPE', type=str, default='http', help='Type of proxy server, i.e. http')
# Get the arguments from the parser
args = parser.parse_args()
# Define formula string if input
if args.formula:
values = {'formula': str(args.fontsize) + '.' + args.formula} # generate formula from args
else:
values = {}
# Define proxy settings if proxy server is input.
if args.proxServ: # set up the proxy server support
proxySupport = ProxyHandler({args.proxType: args.proxServ})
opener = build_opener(proxySupport)
install_opener(opener)
# Set up the data object
data = urlencode(values)
data = data.encode('utf-8')
# Send request to the server and receive response, with error handling!
try:
req = Request(args.webAddr, data)
# Read the response and print to a file
response = urlopen(req)
print response.read()
except URLError, e:
if hasattr(e, 'reason'): # URL error case
# a tuple containing error code and text error message
print 'Error: Failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'): # HTTP error case
# HTTP error code, see section 10 of RFC 2616 for details
print 'Error: The server could not fulfill the request.'
print 'Error code: ', e.code
# print e.read()
如果有人好奇,完整的代码可以在 project page 上找到。一旦我修复了几个额外的错误并对其进行了测试。 (工作的)Windows 版本已经在那里。
