简体   繁体   中英

How to get Python to add a new row in excel every time I run the program?

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:预期产出

You are overwriting the data because every time you run the script you are creating a new empty dataframe and you append the data to that. What you need to do is to firstly load the existing file, append the data to that, save it and repeat.

So in your code replace this 11th line

final_dataframe = pd.DataFrame(columns=my_columns)

with

final_dataframe = pd.read_excel('Testing.xlsx')

I run the program a couple of times and got your desired output:

在此处输入图像描述

Finally you are formatting column A all the way down until the maximum row. Not such a big problem but there are more elegant ways to only format the rows that contain data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM