I am trying to update an existing excel file with Pandas.
This files has several sheets:
- A sheet that contains the raw data. This is the data I want to export from Pandas
- Several sheets using the raw data that contain Pivot tables.
I use this code to update the first sheet of the excel files but keep the other sheets:
disq_df = pd.DataFrame(disqualifications)
writer = pd.ExcelWriter(args.output, engine='openpyxl')
if os.path.exists(args.output):
f, ext = os.path.splitext(args.output)
shutil.copyfile(args.output, f + "-saved" + ext)
writer.book = load_workbook(args.output)
writer.sheets = dict((ws.title, ws) for ws in writer.book.worksheets)
disq_df.to_excel(writer, sheet_name=args.sheet)
writer.save()
This mostly works fine (only the first sheet is modified), except that the Pivot Tables on the others sheets actually standard cells. I also had graphics on some of the sheets that disapeared as well.
Is there a way to keep these sheets totally unmodified? Or is it a limitation of openpyxl
?
This is an extract of the sheet: https://www.dropbox.com/s/dd3jhiif93knh26/Disqualifications.xlsx?dl=0.
The corresponding disq_df
dataframe is just build from dicts:
disq_df = pd.DataFrame({'Année naissance': {0: 2005, 1: 2006},
'Catégorie': {0: 'Poussin', 1: 'Poussin'},
'Club': {0: "CERCLE NAGEURS D'ANTIBES", 1: "CERCLE NAGEURS D'ANTIBES"},
'Compétition': {0: 'Natathlon poussins N° 2', 1: 'Natathlon poussins N° 2'},
'Date': {0: '2016-02-27 15:17:00', 1: '2016-02-27 15:49:50'},
'Disqualification': {0: 'DSQ Vi', 1: 'DSQ Ni'},
'Disqualification (libellé)': {0: 'Disqualifié pour Virage incorrect',
1: 'Disqualifié pour Nage incorrecte'},
'Ligne': {0: 2, 1: 1},
'Nage': {0: 'Nage Libre', 1: 'Dos'},
'Nage (Complet)': {0: '400 Nage Libre Messieurs', 1: '50 Dos Dames'},
'Niveau': {0: 'Compétitions Départementales',
1: 'Compétitions Départementales'},
'Sexe': {0: 'M', 1: 'F'},
'Série': {0: 1, 1: 3}})