use xlwings to run python code in excel

Multi tool use
Multi tool use


use xlwings to run python code in excel



I have a problem running python code by xlwings in excel. My vba code is:


Sub Practice()
RunPython ("import practice; practice.getdata()")
End Sub



My python code is practice.py in pycharm. I use the python code to connect to the deribit api and then use excel to run the python code to download data from deribit api to excel. My python code is the following:


import pprint
import xlwings as xw
import pandas as pd
import numpy as np
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl import Workbook
from deribit_api import RestClient

def getdata():

access_key = "6wvUvxmVSoJq"
access_secret = "HQQ7ZTU2ZESOR2UELLVSHCRWSHPP2VYE"
url = "https://test.deribit.com"
client = RestClient(access_key, access_secret, url)
client.index()
positions = client.positions()
account = client.account()

dfp = pd.DataFrame(columns=['Kind', 'Expiry Date', 'Direction', 'Underlying', 'Delta', 'Size', 'P&L'], index=range(len(positions)))
for i in range(len(positions)):
dfp.loc[i]['Kind'] = positions[i]['kind']
dfp.loc[i]['Expiry Date'] = positions[i]['instrument']
dfp.loc[i]['Direction'] = positions[i]['direction']
dfp.loc[i]['Underlying'] = positions[i]['indexPrice']
dfp.loc[i]['Delta'] = positions[i]['delta']
dfp.loc[i]['Size'] = positions[i]['size']
dfp.loc[i]['P&L'] = positions[i]['profitLoss']

wb = xw.Book.caller()

ws = wb.active

for r in dataframe_to_rows(dfp, index=False, header=True):
ws.append(r)

ws.cell(row=15, column=1).value = 'Total Delta'
ws.cell(row=15, column=2).value = 'Options Delta'
ws.cell(row=15, column=3).value = 'Options Gamma'
ws.cell(row=15, column=4).value = 'Options Theta'
ws.cell(row=15, column=5).value = 'Options Vega'

ws.cell(row=16, column=1).value = account['deltaTotal']
ws.cell(row=16, column=2).value = account['optionsD']
ws.cell(row=16, column=3).value = account['optionsG']
ws.cell(row=16, column=4).value = account['optionsTh']
ws.cell(row=16, column=5).value = account['optionsV']



After running excel, I get an error message like the following:



File "/Users/wenchengwang/PycharmProjects/practice/practice.py", line
7, in



from deribit_api import RestClient



ModuleNotFoundError: No module named 'deribit_api'



I am confused the error message, does it mean I have to install deribit api into excel?




1 Answer
1



You seem to be invoking a Python environment from Excel that does not have the derebit package installed. You have to either point your Excel add-in to the interpreter that has the package installed or add the path of the package to your PYTHONPATH, see:



http://docs.xlwings.org/en/stable/addin.html#global-settings






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

WUXaNQ,6ToSAjKEM6XaX,T
W zF D,wh U

Popular posts from this blog

Rothschild family

Cinema of Italy