如何将数据写入excel中,而不覆盖原有数据

之前直接用pandas库,然后to_excel(),结果直接将原始数据直接覆盖,幸亏有备份。(友善提醒,做数据处理之前,先将数据本地备份一份,确认完全没有问题,然后还是备份一份 ,再进行实盘操作,否则…)

import pandas as pd
import numpy as np
import akshare as ak
from openpyxl import load_workbook,Workbook 
import openpyxl
import akshare as ak
import datetime
Date = datetime.datetime.today().strftime('%Y-%m-%d')
df = pd.read_excel('期权模型.xlsx','参数输入')
jy = df.iloc[len(df)-1,1]
cjy = df.iloc[len(df)-1,2]
jrt = df.iloc[len(df)-1,3]-1
cjrt = df.iloc[len(df)-1,4]-1
#手动输入部分
HV45 = float(input('输入45天HV'))
HV50 = float(input('输入50天HV'))
HV55 = float(input('输入55天HV'))

AHV=np.average([HV45,HV50,HV55])

#从akshare获取数据输入
get_shfe_option_daily_one,get_shfe_option_daily_two = ak.get_shfe_option_daily(trade_date=today, symbol="天胶期权")
ru = get_shfe_option_daily_two
ru = ru[['合约系列','成交量','隐含波动率']]
ru = ru.iloc[[1,5],:].T

jvix=ru.iloc[2,0] 
cjvix=ru.iloc[2,1]
jvol=ru.iloc[1,0]
cjvol=ru.iloc[1,1]
#方案2
filepath = r'橡胶期权量化模型.xlsx'
df = pd.read_excel(filepath,sheet_name='参数输入')
row = len(df.DATE.unique())

wb = openpyxl.load_workbook(filepath)
ws = wb.worksheets[0]

ws.cell(row = row+1,column=1).value = Date
ws.cell(row =  row+1,column=2).value = jy
ws.cell(row =  row+1,column=3).value = cjy
ws.cell(row =  row+1,column=4).value = jrt
ws.cell(row =  row+1,column=5).value = cjrt
ws.cell(row = row+1,column=6).value = HV45
ws.cell(row =  row+1,column=7).value = HV50
ws.cell(row = row+1,column=8).value = HV55
ws.cell(row =  row+1,column=9).value = AHV
ws.cell(row =  row+1,column=10).value = jvix
ws.cell(row = row+1,column=11).value = cjvix
ws.cell(row =  row+1,column=12).value = jvol
ws.cell(row =  row+1,column=13).value = cjvol
wb.save(filepath)

你可能感兴趣的:(量化)