2

MS Word for Windows で使用しているマクロを OSX に移植する作業を行っています。このマクロにより、ユーザーは Word で LaTeX を使用して方程式を生成でき、サーバーに POST 要求を送信して結果を返す必要があります。これは、Windows ではMicrosoft.XMLHTTPオブジェクトを使用して正常に動作しますが、OSX には同等のものはないようです。

これを回避するために、 と を使用urlliburllib2てリクエストを処理する Python スクリプトを作成し、 を使用して LaTeX 文字列と Web アドレスを入力引数として送信できるようにしましたargparse。このスクリプトは、必要な処理を実行し、Web クエリの結果を文字列として返します。

VBA でこの文字列が必要です。

私の現在の VBA 呼び出しは次のようなものです。ここで、pyPathgetURLpathは静的で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 環境に取り込む方法を知っている人はいますか?

4

1 に答える 1

2

質問から抽出された回答:

うまくいきました!myを次のように定義した関数
を使用して、Python スクリプトを呼び出すことができました。result = MacScript(command)command

command = "do shell script """ & pyPath & "python " & getURLpath & "getURL.py --formula '" _
          & Latex_Str & "' --fontsize " & Font_Size & " " & WebAdd & """"

私の Python スクリプトは呼び出され、オプションの引数にgetURL.py基づいてサーバーへの要求を処理し、ユーザーによって定義され、VBA にそれぞれおよび サーバーの Web アドレスとして保存されます。また、プロキシ設定の受け渡しを処理するために、Python スクリプトにいくつかの機能を追加しました。渡された上記のコマンドは、サーバーからの戻り値である Python スクリプトの stdout を返します。Python スクリプトは次のとおりです。--formula--fontsizeLatex_StrFont_SizeWebAddMacScript

# 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()

興味のある方は、いくつかの追加のバグを修正してテストした後、完全なコードをプロジェクト ページで利用できるようにします。(動作中の) Windows バージョンは既にそこにあります。

于 2015-03-01T19:06:55.793 に答える