我的代码在第一次运行时工作正常,该第一次运行创建了基本的excel文件,但是当我第二次运行它时,向文件中添加了另一行会引发错误。
可能是什么原因? 这是styleframe模块中的处理错误还是我必须以其他方式传递数据框才能正确处理
import pandas as pd
import os
from StyleFrame import StyleFrame, Styler, utils
time_val='12oct 23:00'
league_name='sdvsv dvd ragf'
Team1_name='adfvdsr wvd'
Team2_name='dsvdwd rfb'
score_1='4'
score_2='2'
on_target_1='20'
on_target_2='34'
off_target_1='14'
off_target_2='12'
attacks_1='5'
attacks_2='7'
dattacks_1='12'
dattacks_2='23'
possesion_1='5'
possesion_2='61'
def write(time_val,league_name,Team1_name,Team2_name,attacks_1,attacks_2,
dattacks_1,dattacks_2,on_target_1,on_target_2,off_target_1,off_target_2,
possesion_1,possesion_2,score_1,score_2):
if os.path.exists(league_name+'.xlsx'):
df=pd.read_excel(league_name+'.xlsx')
df2=pd.DataFrame([[time_val,league_name,Team1_name,str(score_1)+'-'+str(score_2),
Team2_name,str(on_target_1)+'-'+str(on_target_2),str(off_target_1)+'-'+str(off_target_2),
str(attacks_1)+'-'+str(attacks_2),str(dattacks_1)+'-'+str(dattacks_2),
str(possesion_1)+'-'+str(possesion_2)],],
columns='Time,League,Team1,Score,Team2,Shots on target,Off target,Attacks,Dangerous attacks,Possesion'.split(','))
new_df=df.append(df2)
print(df)
print(df2)
print(new_df)
sf2 = StyleFrame(new_df)
header_values=new_df.columns.tolist()
sf2.set_column_width(header_values,18)
sf2.apply_headers_style(styler_obj=Styler(font_size=8,wrap_text=False,shrink_to_fit=False,
bg_color='#FFFF33',horizontal_alignment=utils.horizontal_alignments.center,bold=True,
border_type=utils.borders.medium))
sf2.apply_column_style(cols_to_style=header_values,styler_obj=Styler(font_size=8,wrap_text=False,shrink_to_fit=False,
bg_color='#DCDCDC',horizontal_alignment=utils.horizontal_alignments.center,
border_type=utils.borders.medium))
writer = pd.ExcelWriter(league_name+'.xlsx')
row=0
sf2.to_excel(writer,startrow=row , startcol=0,index=False).save()
else:
writer = pd.ExcelWriter(league_name+'.xlsx')
row=0
df2=pd.DataFrame([[time_val,league_name,Team1_name,str(score_1)+'-'+str(score_2),
Team2_name,str(on_target_1)+'-'+str(on_target_2),str(off_target_1)+'-'+str(off_target_2),str(attacks_1)+'-'+str(attacks_2),str(dattacks_1)+'-'+str(dattacks_2),
str(possesion_1)+'-'+str(possesion_2)],],
columns='Time,League,Team1,Score,Team2,Shots on target,Off target,Attacks,Dangerous attacks,Possesion'.split(','))
new_df=df2.copy()
sf2 = StyleFrame(new_df)
header_values=new_df.columns.tolist()
sf2.set_column_width(header_values,18)
sf2.apply_headers_style(styler_obj=Styler(font_size=8,wrap_text=False,shrink_to_fit=False,
bg_color='#FFFF33',horizontal_alignment=utils.horizontal_alignments.center,bold=True,
border_type=utils.borders.medium))
sf2.apply_column_style(cols_to_style=header_values,styler_obj=Styler(font_size=8,wrap_text=False,shrink_to_fit=False,
bg_color='#DCDCDC',horizontal_alignment=utils.horizontal_alignments.center,
border_type=utils.borders.medium))
sf2.to_excel(writer,startrow=row , startcol=0,index=False).save()
write(time_val,league_name,Team1_name,Team2_name,attacks_1,attacks_2,dattacks_1,dattacks_2,on_target_1,on_target_2,off_target_1,off_target_2,possesion_1,possesion_2,score_1,score_2)