Python Forum
How to add a dataframe to an existing excel file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to add a dataframe to an existing excel file
#1
Hello,

I am trying to add a dataframe to an existing sheet. I don't see any error, but the data does not show on the sheet.

Is anyone able to tell me what am I missing?

Thank you!

#Grab dataframe from another excel sheet
df = pd.read_excel('C:/path/Dataframe_File.xlsx',sheetname='Data') 

#Open existing excel file
workbook1 = openpyxl.load_workbook('C:/path/Existing_File.xlsx')

writer = pd.ExcelWriter('C:/path/Existing_File.xlsx', engine='openpyxl') 

#Add dataframe to excel file 
df.to_excel(writer,sheet_name="Sheet",index=False,startrow=17,startcol=1,engine='openpyxl')

workbook1.save('C:/path/Existing_File.xlsx')
Reply
#2
It's just a guess but maybe you need administrator priviledges to write on that path (e.g. I have to run command prompt as administrator to use "pip install" just because it's on C drive, maybe that is also the case with saving your file)
Reply
#3
I found a way around like this.

from openpyxl import load_workbook
workbook1 = openpyxl.load_workbook('file.xlsx')

writer = pd.ExcelWriter('file.xlsx', engine='openpyxl') 
writer.book = workbook1
new_file.to_excel(writer, sheet_name='Shee1',index=False,startrow=2,startcol=1)

writer.save()
writer.close()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Export dataframe to xlsx - Error "zipfile.BadZipFile: File is not a zip file" Baggio 10 18,018 Mar-12-2021, 01:02 PM
Last Post: buran
  Python - Merge existing cells of Excel file created with xlsxwriter manonB 0 1,278 Mar-10-2021, 02:17 PM
Last Post: manonB
  Creating more than one excel File at once malvarez1976 0 772 Dec-15-2020, 02:04 AM
Last Post: malvarez1976
  How to form a dataframe reading separate dictionaries from .txt file? Doug 1 2,636 Nov-09-2020, 09:24 AM
Last Post: PsyPy
  Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 2,744 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  Convert Excel to .txt - Need each excel row to be separate .txt file cowboykevin05 2 2,745 Jan-03-2020, 06:29 PM
Last Post: Larz60+
  [split] Converting excel file to txt file unexceptionalhobby 2 2,783 Oct-16-2019, 06:34 PM
Last Post: unexceptionalhobby
  Python write result of VAR to excel file wissam1974 8 4,699 Jul-13-2019, 01:09 PM
Last Post: wissam1974
  Is there any way to properly load fixed width file into a dataframe using Pandas? vicky53 1 1,908 Mar-29-2019, 06:04 PM
Last Post: Larz60+
  convert images into pixel dataframe into csv file using python synthex 3 14,715 Feb-17-2019, 06:26 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020