I have a function thats collects data from a sandbox api, and writes this data into a ecxel sheet using pandas and xlsxwriter combinded. My question is how I can get Python to write the new data into a new row every time I run the program? Now it's just overwriting the old data every time i run the program over again.
This is my code:
import pandas as pd
import xlsxwriter
import requests
def collection():
api_url = f'https://sandbox.millistream.com/mws.fcgi?usr=sandbox&pwd=sandbox&cmd=quote&list=39970\
&fields=symbol,name,diff1dprc,lastprice,time&orderby=symbol&order=asc&timezone=Europe/Oslo&filetype=json'
data = requests.get(api_url).json()
my_columns = ['Time', 'Stocks price']
final_dataframe = pd.DataFrame(columns=my_columns)
final_dataframe = final_dataframe.append(
pd.Series(
[
data[2]['time'],
data[2]['lastprice']
],
index=my_columns),
ignore_index=True
)
writer = pd.ExcelWriter('Testing.xlsx', engine='xlsxwriter')
final_dataframe.to_excel(writer, 'AKER', index=False)
background_color = '#000000'
font_color = '#E8130F'
string_format = writer.book.add_format(
{
'font_color': font_color,
'bg_color': background_color,
'border': 1
}
)
dollar_format = writer.book.add_format(
{
'num_format': '$0.00',
'font_color': font_color,
'bg_color': background_color,
'border': 1
}
)
writer.sheets['AKER'].set_column('A1:A506', 20, string_format)
writer.sheets['AKER'].set_column('B1:B506', 10, dollar_format)
writer.save()
This is the return in Excel:
And this is how I want the return to look: