2017-09-16 91 views
0

我最近在我的Mac安裝xlwings,我目前試圖寫一個小程序來更新一些數據(通過請求)。作爲一項測試,我試圖通過API更新cryptocurrency價格並將它們寫入excel。 不使用runpython,代碼有效。但是當我跑我的VBA代碼, 我得到這個錯誤:xlwings runpython EOL錯誤

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 
+0

從您發佈的錯誤的外觀上來看,你可能會丟失從一個字符串賦值 – jsotola

+0

對於我來說,似乎更多的是單引號字符,有一些錯誤的文件路徑傳遞的方式。然而,我沒有絲毫的線索知道'import sys [..]'錯誤是指什麼,或者它試圖對我說什麼?正如你在我的代碼中看到的,我沒有使用sys模塊。 – ohlawd

+0

編輯:我剛剛下載了xlwings斐波那契示例文件,它引發了完全相同的錯誤。可能是PYTHONPATH的東西?問題,我在這方面不是很有經驗.. – ohlawd

回答

0

解決了這個。我只是想爲任何可能面臨同樣問題的人發佈解決方案。

我去檢查我的xlwings.conf文件,才能看到「翻譯風波」和「PYTHONPATH」設置。我從來沒有對此進行過編輯,但它的格式不正確。

正確格式爲:

"INTERPRETER","pythonw" 
"PYTHONPATH","" 

我的配置文件是安裝程序是這樣的:

"PYTHONPATH"," 
" 
"INTERPRETER","Python" 

此外,被默認設置爲我的Python的路徑不正確。雖然我的命令行與蟒蛇蟒蛇3.6作品「pythonw」使用的解釋中引用到Python 2.7裏面傳來預裝和MacOS .bash_profile中設置。 編輯配置文件「INTERPRETER」解決了這個問題。

謝謝大家。