0

I want to write a data frame into an existing Excel sheet without overwriting and save it using another file name using Python

import pandas as pd
import openpyxl

srcfile = openpyxl.load_workbook('C:\\Users\\kavitha.j\\Desktop\\Automation\\Report builder\\template.xlsx',read_only=False, keep_vba= True)*#to open the excel sheet and if it has macros*

df=pd.read_excel('C:\\Users\\kavitha.j\\Desktop\\Automation\\Report builder\\CVS Report Builder test.xlsx',sheet_name='Landing Page',skiprows=8)
df
sheetname = srcfile.get_sheet_by_name('Sheet1')#get sheetname from the file
sheetname.range('A9').value = df
sheetname.range('A9').options(pd.DataFrame, expand='table').value
srcfile.save('C:\\Users\\kavitha.j\\Desktop\\Automation\\Report builder\\finaltemplate.xlsm') #save it as a new file, the original file is untouched and here I am saving it as xlsm(m here denotes macros).
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • pls format code – Ahmet Jan 27 '20 at 06:57
  • From what I understand [Append existing excel sheet with new dataframe using python pandas](https://stackoverflow.com/questions/38074678/append-existing-excel-sheet-with-new-dataframe-using-python-pandas) might help you – anky Jan 27 '20 at 06:58

0 Answers0