我最近在我的Mac上安装了xlwing,目前我正在尝试编写一个小程序来更新一些数据(通过请求)。作为一种测试,我尝试通过API更新加密货币价格,并将它们写入excel。不使用runpython,代码就能工作。然而,一旦我运行我的VBA代码,我就会得到以下错误:
File "<string>", line 1
import sys, os;sys.path.extend(os.path.normcase(os.path.expandvars('/Users/Dennis/Documents/crypto;
^
SyntaxError: EOL while scanning string liberal我搜索了无数的帖子和论坛,但似乎找不到我的问题的答案。为了更好的理解,
我的python代码:
import requests, json
from datetime import datetime
import xlwings as xw
def do():
parameter = {'convert' : 'EUR'}
#anfrage über API
query_ticker = requests.get('https://api.coinmarketcap.com/v1/ticker', params = parameter)
#anfragedaten in JSON-format
data_ticker = query_ticker.json()
wb = xw.Book.caller()
ws0 = wb.sheets['holdings']
for entry in data_ticker:
# update eth price
if entry['symbol'] == 'ETH':
ws0.range('B14').value = float(entry['price_eur'])
#update btc price
if entry['symbol'] == 'BTC':
ws0.range('B15').value = float(entry['price_eur'])
if entry['symbol'] == 'NEO':
ws0.range('B16').value = float(entry['price_eur'])
if entry['symbol'] == 'XRP':
ws0.range('B17').value = float(entry['price_eur'])
now = datetime.now()
write_date = '%s.%s.%s' %(now.day, now.month, now.year)
write_time = '%s:%s:%s' %(now.hour, now.minute,now.second)
ws0.range('B2').value = write_date
ws0.range('B3').value = write_time
wb.save('holdings.xlsm')
#wb.close()这是我的vba代码:
Sub update_holdings()
RunPython ("import update_holdings; update_holdings.do()")
End Sub发布于 2017-09-18 09:57:46
解决了这个问题。我只是想为任何可能面临同样问题的人发布解决方案。
为了查看“解释器”和"PYTHONPATH“的设置,我检查了我的xlwings.conf文件。我从来没有对此进行过编辑,但是,它的格式是错误的。
正确的格式是:
"INTERPRETER","pythonw"
"PYTHONPATH",""我的配置文件是这样设置的:
"PYTHONPATH","
"
"INTERPRETER","Python"此外,默认情况下,我的python的路径设置不正确。尽管我的命令行可以使用Anacondapython3.6,但"pythonw“使用了.bash_profile中的解释器集,该解释器集引用了macOS预装的python2.7。编辑配置文件“解释器”解决了这个问题。
谢谢大家。
https://stackoverflow.com/questions/46256267
复制相似问题