Problem
I need to make a python script that can export information to excel sheet without deleting sheets old information or sheets. See Pseudocode
Context:
This function is to be used in conjunction with a web scraper. And should hopefully, be able to extend data to an existing sheet, create a new sheet, and create a new file.
With the help of other questions, I have come really close: How to save a new sheet in an existing excel file, using Pandas? But I want the possibility to extend on existing sheets.
Pseudocode
def OutputDataToExcel(Filename,Sheetname,df): #df is a pandas dataframe
If Filename and Sheetname exists:
extend the Sheetname with df (no header)
elif Filename exists:
create new sheet with Sheetname
add df (with header)
else:
create new excelfile with Filename
create new sheet with Sheetname
add df (with header)
My code so far
Some is out commented to focus on the problem
import openpyxl
import pandas as pd
def outputdata(Filename,Sheetname,df):
#try:
#will error if file or sheet cannot be found
#And will extend information to the sheet
xl1 = pd.read_excel(Filename,Sheetname)
xl2 = pd.ExcelFile(Filename)
wb=openpyxl.load_workbook(Filename)
length = len(xl2.parse(Sheetname))
writer = pd.ExcelWriter(Filename,sheet_name = Sheetname , engine = "openpyxl")
df.to_excel(writer,Sheetname, startrow=length+1, index=False, header = None)
writer.book = wb
writer.save()
#writer.close()
#except:
#try:
#will error if file cannot be found
#and will create a new sheet with information
#wb=openpyxl.load_workbook(Filename)
#wb.openpyxl.create_sheet(Sheetname)
#df.to_excel(writer,Sheetname, index=False)
#writer.save()
#writer.close()
#except:
#writer = pd.ExcelWriter(Filename,sheet_name = Sheetname , engine = "openpyxl")
#df.to_excel(writer,Sheetname, index= False)
#writer.save()
#writer.close()
The result
A new sheet is created with the name Sheetname1 and df is added to this sheet, insted of it being added to Sheetname. If I run it again a new sheet named Sheetname2 and df is then added here...
I hope someone can help me, it's my first question so, please respond both with help or critic on how to present the problem.