Python How to use ExcelWriter to write into an existing worksheet - python

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.

Related

pandas dataframe not posting correctly to excel

I've an excel sheet "Calcs" with 1 column name "old". I'm trying to add new column "new" with a fixed value of "1" to existing sheet "Calcs" and am using below code which is resulting 2 issues.
it's not updating existing sheet rather it's creating new sheet called "Calcs1"
After code is executed and while opening excel file, getting this error. (no such error while opening file before execution of the code).
We found a problem with some content in 'test1.xlsx'. Do you want us
to try to recover as much as we can? if you trust the source of this
workbook, click Yes.
Appreciate any help
import pandas as pd
from openpyxl import load_workbook
file = r"C:\test1.xlsx"
df2 = pd.read_excel(file, sheet_name = 'Calcs')
df2["new"] = "1"
book = load_workbook(file)
writer = pd.ExcelWriter(file, engine = 'openpyxl')
writer.book = book
df2.to_excel(writer, sheet_name = 'Calcs')
writer.save()
writer.close()

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)

Overwrite an excel sheet with pandas dataframe without affecting other sheets

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.

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.

import csv to xlsx python

I'm trying to put some data from a csv file to exist excel file.
my exist excel file contains images and xlrd cannot get images.
I try to use xlsxwriter but it cannot append to existing xslx.
the only solution I've found is to use openpyxl.
import openpyxl
xfile = openpyxl.load_workbook('my_exist_file')
sheet = xfile.get_sheet_by_name('Sheet1')
with open("my_csv", 'rb') as f:
reader = csv.reader(f)
for r, row in enumerate(reader):
for c, col in enumerate(row):
-here is my problem-
how can I write the csv data (that is a table) to a specific location in the exist xslx? I want that my table will start at K2 cell.
thanks!
reading the CSV
using pandas.read_csv to extract the information
import pandas as pd
df = pd.read_csv(my_filename)
Options you might need to specify
sep: which separator is used
encoding
header: Is the first row a row of labels?
index_col: is the first column an index
adding to an excel worksheet
inspired by: https://stackoverflow.com/a/20221655/1562285
check the pandas.to_excel documentation for other possible options
book = load_workbook(old_filename)
sheet_name = 'Sheet1'
with pd.ExcelWriter(new_filename, engine='openpyxl') as writer:
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, sheet_name=sheet_name, startrow=1, startcol=10, engine='openpyxl')
The startrow and startcol say where in the worksheet you want to paste your data.
This method might overwrite the previous content on this worksheet. If it does, you will have to loop over the columns and rows of the DataFrame and add them semi-manually to the worksheet
Inserting images
If you have the images to insert somewhere externally you can use the code from the documentation
from openpyxl.drawing.image import Image
ws = book['sheet_name_for_images']
ws['A1'] = 'You should see three logos below'
img = Image('logo.png')
# add to worksheet and anchor next to cells
ws.add_image(img, 'A1')
I did not test this, and you might need to insert this code before the writer.sheets = ...
Use the worksheet's cell method to update a specific cell
sheet.cell(row=<row>, column=<col>, value=<val>)
It is usually a good idea to use keep_vba=True while loading workbook. Check the help page for more details.
Also check answer to this question.

Categories