PYTHON DICTIONARY TO NEW COLUMNS AND ROWS IN EXISTING EXCEL FILE Current Code: import openpyxl import json import reques
Posted: Mon Jun 06, 2022 11:03 am
PYTHON DICTIONARY TO NEW COLUMNS AND ROWS IN EXISTING
EXCEL FILE
Current Code:
import openpyxl
import json
import requests
path = 'C:\\Users\\moses\\Desktop\\Program
Projects\\merger\\allstockdata.xlsx'
wb = openpyxl.load_workbook(path)
ws = wb['completestocktable']
x = []
for cell in ws['A']:
datadict = {}
keys = ['symbol', 'period', 'priceToSalesRatio',
'debtToEquity', 'pbRatio', 'revenuePerShare', 'freeCashFlowYield',
'roic', 'roe', 'netIncomePerShare', 'debtToAssets', 'peRatio',
'researchAndDdevelopementToRevenue', 'netIncomeGrowth',
'epsgrowth', 'revenueGrowth', 'freeCashFlowGrowth']
alldata =
json.loads(requests.get('https://financialmodelingprep.com/api/v3/key-metrics/'
+ cell.value +
'?period=quarter&limit=130&apikey=d32d97c8217d40ed4dd4118e8dd6dbff').text)
+
json.loads(requests.get('https://financialmodelingprep.com/api/v ... al-growth/'
+ cell.value +
'?period=quarter&limit=80&apikey=d32d97c8217d40ed4dd4118e8dd6dbff').text)
if len(alldata) == 0: #No data on a stock in the
API
alldata = None
else:
for a in range(len(alldata)):
for key, val in
alldata[a].items():
if key in
keys:
datadict[key] = val
print(datadict)
Result from an interation in code:
{'symbol': 'ALLO', 'period': 'Q2', 'revenuePerShare':
0.0, 'netIncomePerShare': -0.38259644193137377, 'peRatio':
-16.335750454054303, 'priceToSalesRatio': None, 'pbRatio':
-16.873509436426566, 'freeCashFlowYield': -0.006511448928269935,
'debtToEquity': -0.15234233538941958, 'debtToAssets':
1.387016209633876, 'researchAndDdevelopementToRevenue': None,
'roic': -0.08045822786646262, 'roe': 0.25822978693089055,
'revenueGrowth': 0.0, 'netIncomeGrowth': -50.94532152483635,
'epsgrowth': -19.10893438177874, 'freeCashFlowGrowth':
-1317.6}
PROBLEM:
In line 9: the cell values in column A are stock ticker
symbols like ALLO in the result section. I am iterating through all
stock ticker symbols in column A. How can I create
additional columns in the .xlsx file (Line4) from the keys in
Datadict and their respective values as cell values in each column
for each stock ticker symbol using python? I can edit out the extra
column of symbols later.
Example of Psuedo Desired Column and Row for Result
using first 3 key:value pairs in the dictionary(Refer back to
Result section):
EXCEL FILE
Current Code:
import openpyxl
import json
import requests
path = 'C:\\Users\\moses\\Desktop\\Program
Projects\\merger\\allstockdata.xlsx'
wb = openpyxl.load_workbook(path)
ws = wb['completestocktable']
x = []
for cell in ws['A']:
datadict = {}
keys = ['symbol', 'period', 'priceToSalesRatio',
'debtToEquity', 'pbRatio', 'revenuePerShare', 'freeCashFlowYield',
'roic', 'roe', 'netIncomePerShare', 'debtToAssets', 'peRatio',
'researchAndDdevelopementToRevenue', 'netIncomeGrowth',
'epsgrowth', 'revenueGrowth', 'freeCashFlowGrowth']
alldata =
json.loads(requests.get('https://financialmodelingprep.com/api/v3/key-metrics/'
+ cell.value +
'?period=quarter&limit=130&apikey=d32d97c8217d40ed4dd4118e8dd6dbff').text)
+
json.loads(requests.get('https://financialmodelingprep.com/api/v ... al-growth/'
+ cell.value +
'?period=quarter&limit=80&apikey=d32d97c8217d40ed4dd4118e8dd6dbff').text)
if len(alldata) == 0: #No data on a stock in the
API
alldata = None
else:
for a in range(len(alldata)):
for key, val in
alldata[a].items():
if key in
keys:
datadict[key] = val
print(datadict)
Result from an interation in code:
{'symbol': 'ALLO', 'period': 'Q2', 'revenuePerShare':
0.0, 'netIncomePerShare': -0.38259644193137377, 'peRatio':
-16.335750454054303, 'priceToSalesRatio': None, 'pbRatio':
-16.873509436426566, 'freeCashFlowYield': -0.006511448928269935,
'debtToEquity': -0.15234233538941958, 'debtToAssets':
1.387016209633876, 'researchAndDdevelopementToRevenue': None,
'roic': -0.08045822786646262, 'roe': 0.25822978693089055,
'revenueGrowth': 0.0, 'netIncomeGrowth': -50.94532152483635,
'epsgrowth': -19.10893438177874, 'freeCashFlowGrowth':
-1317.6}
PROBLEM:
In line 9: the cell values in column A are stock ticker
symbols like ALLO in the result section. I am iterating through all
stock ticker symbols in column A. How can I create
additional columns in the .xlsx file (Line4) from the keys in
Datadict and their respective values as cell values in each column
for each stock ticker symbol using python? I can edit out the extra
column of symbols later.
Example of Psuedo Desired Column and Row for Result
using first 3 key:value pairs in the dictionary(Refer back to
Result section):