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
  Pandas keep existing format of Excel AlphaInc 2 878 Jan-11-2024, 03:44 AM
Last Post: plonkarchivist
  Data Sorting and filtering(From an Excel File) PY_ALM 0 1,012 Jan-09-2023, 08:14 PM
Last Post: PY_ALM
  export dataframe to file.txt dramauh 5 1,884 Apr-21-2022, 01:23 AM
Last Post: sarahroxon7
  Export dataframe to xlsx - Error "zipfile.BadZipFile: File is not a zip file" Baggio 10 61,378 Mar-12-2021, 01:02 PM
Last Post: buran
  Python - Merge existing cells of Excel file created with xlsxwriter manonB 0 3,686 Mar-10-2021, 02:17 PM
Last Post: manonB
  Creating more than one excel File at once malvarez1976 0 1,791 Dec-15-2020, 02:04 AM
Last Post: malvarez1976
  How to form a dataframe reading separate dictionaries from .txt file? Doug 1 4,197 Nov-09-2020, 09:24 AM
Last Post: PsyPy
  Convert Excel to .txt - Need each excel row to be separate .txt file cowboykevin05 2 4,723 Jan-03-2020, 06:29 PM
Last Post: Larz60+
  [split] Converting excel file to txt file unexceptionalhobby 2 4,308 Oct-16-2019, 06:34 PM
Last Post: unexceptionalhobby
  Python write result of VAR to excel file wissam1974 8 8,508 Jul-13-2019, 01:09 PM
Last Post: wissam1974

Forum Jump:

User Panel Messages

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