Overwrite an excel sheet with pandas dataframe without affecting other sheets - python

I want to overwrite an existing sheet in an excel file with Pandas dataframe but don't want any changes in other sheets of the same file. How this can be achieved.
I tried below code but instead of overwriting, it is appending the data in 'Sheet2'.
import pandas as pd
from openpyxl import load_workbook
book = load_workbook('sample.xlsx')
writer = pd.ExcelWriter('sample.xlsx', engine = 'openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, 'sheet2', index = False)
writer.save()

I didn't find any other option other than this, this would be a quick solution for you.
I believe still there's no direct way to do this, correct me if I'm wrong. That's the reason we need to play with these logical ways.
import pandas as pd
def write_excel(filename,sheetname,dataframe):
with pd.ExcelWriter(filename, engine='openpyxl', mode='a') as writer:
workBook = writer.book
try:
workBook.remove(workBook[sheetname])
except:
print("Worksheet does not exist")
finally:
dataframe.to_excel(writer, sheet_name=sheetname,index=False)
writer.save()
df = pd.DataFrame({'Col1':[1,2,3,4,5,6], 'col2':['foo','bar','foobar','barfoo','foofoo','barbar']})
write_excel('PRODUCT.xlsx','PRODUCTS',df)
Let me know if you found this helpful, or ignore it if you need any other better solution.

Similar to Gavaert's answer... For Pandas 1.3.5, add the 'if_sheet_exists="replace"' option:
import pandas as pd
with pd.ExcelWriter("file.xlsx", engine="openpyxl", mode="a", if_sheet_exists="replace") as writer:
df.to_excel(writer, 'Logs', index=False)

Since Pandas version 1.3.0 on_sheet_exists is an option of ExcelWriter. It can be used as such:
import pandas as pd
with pd.ExcelWriter("my_sheet.xlsx",engine="openpyxl",mode="a",on_sheet_exists="replace") as writer:
pd.write_excel(writer,df)
Since none of the ExcelWriter methods or properties are public, it is advised to not use them.

Related

python writing a df to a specific cell of excel

I have come across a lot of answers and just wanted to check if this is the best answer
Write pandas dataframe values to excel to specific cell in a specific sheet.
The question is - assuming I have a dataframe "df".
I want to write to an existing excel file called "Name1.xlsx", in
worksheet called "exampleNames", and starting at cell d25.
What's the easiest/ most efficient way to do that.
###############Updated!#############
I tried this
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import openpyxl
path = "C:\\Users\\ABC\\PycharmProjects\\ABC\\Name1.xlsx"
df = pd.DataFrame(np.random.randint(1,10,(3,2)),columns=['a','b'])
df.to_excel(path,sheet_name="exampleNames",startcol=5,startrow=5,header=None,index=False)
df.to_excel(path,sheet_name="NN",startcol=5,startrow=25,header=None,index=False)
gave me error
ModuleNotFoundError: No module named 'openpyxl'
This is the approach suggested in the pandas docs
df.to_excel(writer, sheet_name='Sheet1', startcol=col,startrow=row, header=None)
where writer could be path-like, file-like, or ExcelWriter object
eg
df.to_excel('sample.xlsx',sheet_name="exampleNames",startcol=5,startrow=5,header=None)
To save multiple dataframes in excel, you will have to use the writer object
with pd.ExcelWriter('output.xlsx', engine="openpyxl", mode='a', if_sheet_exists='overlay') as writer:
df1.to_excel(writer, sheet_name='exampleNames' ,startcol=5,startrow=5,header=None,index=False)
df2.to_excel(writer, sheet_name='NN', startcol=5,startrow=25,header=None,index=False)

Exporting a pandas df into an excel file that has formatting already in it?

I've spent hours researching this issue but cant seem to find an answer. I have a template in Excel that has conditional formatting already applied to it. I want to import a pandas df into this already formatted excel file so that the data is being formatted accordingly (color, number format, etc.). Does anyone if this is doable? And if so, how?
Ive considered writing a macro and just importing it into python and applying to the df. Just want to see if there's an easier way that I haven't thought of/found. Thanks!
I would advise to try openpyxl
from openpyxl import load_workbook
book = load_workbook(excelpath) # load excel with formats
writer = pandas.ExcelWriter(excelpath, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, "Sheet1", columns=['a', 'b'], index=False) # only columns 'a' and 'b' will be populated
writer.save()

Copying a list to existing excel python

I have multiple lists in my python code and i want to copy those lists to different columns in an already existing excel file.
writer = pd.ExcelWriter('sample.xlsx')
pd.DataFrame(timedata).to_excel(writer, 'timedata')
writer.save()
this writes the list to the excel but it always over writes the data in excel and to write multiple lists in multiple columns is not been defined in this code.
Pandas uses openpyxl for xlsx files(mentioned in pd docs). By checking docs for ExcelWriter, you can see that something like this might work out:
import pandas
from openpyxl import load_workbook
book = load_workbook('sample.xlsx')
writer = pandas.ExcelWriter('sample.xlsx', engine='openpyxl')
writer.book = book
## ExcelWriter for some reason uses writer.sheets to access the sheet.
## If you leave it empty it will not know that sheet Main is already there
## and will create a new sheet.
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
#data_filtered is a pd dataframe
data_filtered.to_excel(writer, "Main", cols=['col1', 'col2'])
writer.save()
If you are using pandas version later than 0.24, then the process is even more simplified:
import pandas as pd
with pd.ExcelWriter('sample.xlsx', engine='openpyxl', mode='a') as writer:
data_filtered.to_excel(writer)

XlsxWriter write data frame starting from specific cell

I'm using xlsxwriter to create an Excel file. Currently, I'm doing something like this:
import datetime
import pandas as pd
import xlsxwriter
workbook = xlsxwriter.Workbook('Test.xlsx')
worksheet = workbook.add_worksheet()
datetime_format = workbook.add_format({
'font_size': 12
})
worksheet.write('C2', datetime.datetime.now().strftime("%Y-%m-%d %H:%M"), datetime_format)
# Rest removed for brevity. I simply have many stuff that
# I manually write to make the design as I want.
workbook.close()
writer = pd.ExcelWriter('Test.xlsx', engine='xlsxwriter')
result.to_excel(writer, startrow=6, startcol=3) # Start from D7.
writer.save()
Now, this seems to rewrite everything that I previously wrote manually using xlsxwriter. Also, I noticed that xlsxwriter does not have support by default to write data frames, am I right? How can I combine xlsxwriter and pandas to manually write some stuff to do Excel sheet, and then later to write all the data frame stuff starting from the specific cell?
You need to shift things around. I would also recommend to read the XlsWritter documentation on how to use it with pandas.
writer = pd.ExcelWriter('Test.xlsx', engine='xlsxwriter')
workbook = writer.book
result.to_excel(writer, sheet_name='Sheet1', index=False, startrow=6, startcol=3) # Start from D7.
worksheet = writer.sheets['Sheet1']
datetime_format = workbook.add_format({
'font_size': 12
})
worksheet.write('C2', datetime.datetime.now().strftime("%Y-%m-%d %H:%M"), datetime_format)
writer.save()
First, we create our writer object using pd.ExcelWriter() and passing xlswriter as the engine. Once we have our writer we can access our workbook using the .book class vriable. At this time, we can write our data into our workbook using df.to_excel(). We will use the sheet_name argument of to_excel to create and name our worksheet. At this point we can acess our worksheet using the .sheets class variable and passing our sheetname as a key - in our case Sheet1.
From there you can write additional values to your sheet with or without using a pandas object - as shown in the code below.

Python How to use ExcelWriter to write into an existing worksheet

I am trying to use ExcelWriter to write/add some information into a workbook that contains multiple sheets.
First time when I use the function, I am creating the workbook with some data. In the second call, I would like to add some information into the workbook in different locations into all sheets.
def Out_Excel(file_name,C,col):
writer = pd.ExcelWriter(file_name,engine='xlsxwriter')
for tab in tabs: # tabs here is provided from a different function that I did not write here to keep it simple and clean
df = DataFrame(C) # the data is different for different sheets but I keep it simple in this case
df.to_excel(writer,sheet_name = tab, startcol = 0 + col, startrow = 0)
writer.save()
In the main code I call this function twice with different col to print out my data in different locations.
Out_Excel('test.xlsx',C,0)
Out_Excel('test.xlsx',D,10)
But the problem is that doing so the output is just the second call of the function as if the function overwrites the entire workbook. I guess I need to load the workbook that already exists in this case?
Any help?
Use load_book from openpyxl - see xlsxwriter and openpyxl docs:
import pandas as pd
from openpyxl import load_workbook
book = load_workbook('test.xlsx')
writer = pd.ExcelWriter('test.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, sheet_name='tab_name', other_params)
writer.save()
Pandas version 0.24.0 added the mode keyword, which allows you to append to excel workbooks without jumping through the hoops that we used to have to do. Just use mode='a' to append sheets to an existing workbook.
From the documentation:
with ExcelWriter('path_to_file.xlsx', mode='a') as writer:
df.to_excel(writer, sheet_name='Sheet3')
You could also try using the following method to create your Excel spreadsheet:
import pandas as pd
def generate_excel(csv_file, excel_loc, sheet_):
writer = pd.ExcelWriter(excel_loc)
data = pd.read_csv(csv_file, header=0, index_col=False)
data.to_excel(writer, sheet_name=sheet_, index=False)
writer.save()
return(writer.close())
Give this a try and let me know what you think.

Categories