Pasting a dataframe into excel using openpyxl - python

I have a table which is the output of a sql query and I want this table to be pasted into a specific cell of an excel (say B10).Using openpyxl I used to do
ws_hi["c31"].value = output_total.iloc[0]
(ws_hi is the excel sheet and output_total is the variable which holds the data I wanna copy)
which works fine for a single value but not for a table. Need help in exporting if output_total is a 4*5 table
FYI output_total is dataframe obtained by
output_total = pd.read_sql_query(text(query), engine1)
thanks!

Since your data is stored in a pandas DataFrame, you can use pd.DataFrame.to_excel and specify the upper left cell where you want to dump your data with startrow and startcol.
In your case this would be something like the following for the B10 cell:
output_total.to_excel('excel/file/path.xlsx', startrow=11, startcol=1, header=False, index=False)

Related

Struggling to append dataframe to existing .xlsx file in Python

I am trying to append a dataframe to an existing excel spreadsheet, but I am having trouble appending it to an existing SHEET (my excel file only has one sheet, titled "Sheet1," that contains the existing dataset).
with pd.ExcelWriter(xlsx_path, mode="a", engine="openpyxl",sheet_name="Sheet1",if_sheet_exists="overlay") as writer:
transfer.to_excel(writer,header=None,index=False)
When I use the aforementioned code, when I open the existing spreadsheet, the new data from the dataframe I requested to be appended via the to_excel function appears in a separate sheet, entitled "Sheet 11." Can someone elucidate why this is occurring? How can I just get the new data from the dataframe to appear at the bottom of the existing spreadsheet in Sheet1?
Thanks!
Refer to notes written above.
I dont know why the data is appended to 'Sheet11', however 'sheet_name=' is not an attribute in ExcelWriter so you should get a warning about that. The attribute should be used with 'to_excel'.
You'll need to state what row to append from otherwise the new data will start from row 1 over-writting any existing data. You can get the max row for the sheet and use that.
sheet_to_update = 'Sheet1'
with pd.ExcelWriter(xlsx_path,
mode="a",
engine="openpyxl",
if_sheet_exists="overlay") as writer:
transfer.to_excel(writer,
header=None,
index=False,
sheet_name=sheet_to_update,
startrow=writer.sheets[sheet_to_update].max_row)

How to export python dataframe into existing excel sheet and retain formatting?

I am trying to export a dataframe I've generated in Pandas to an Excel Workbook. I have been able to get that part working, but unfortunately no matter what I try, the dataframe goes into the workbook as a brand new worksheet.
What I am ultimately trying to do here is create a program that pulls API data from a website and imports it in an existing Excel sheet in order to create some sort of "live updating excel workbook". This means that the worksheet already has proper formatting, vba, and other calculated columns applied, and all of this would ideally stay the same except for the basic data in the dataframe I'm importing.
Anyway to go about this? Any direction at all would be quite helpful. Thanks.
Here is my current code:
file='testbook.xlsx'
writer = pd.ExcelWriter(file, engine = 'xlsxwriter')
df.to_excel(writer, sheet_name="Sheet1")
workbook = writer.book
worksheet = writer.sheets["Sheet1")
writer.save
In case u have both existing excel file and DataFrame in same format then you can simply import your exiting excel file into another DataFrame and concat both the DataFrames then save into new excel or existing one.
df1["df"] = pd.read_excel('testbook.xlsx')
df2["df"] = 1#your dataFrame
df = pd.concat([df1, df2])
df.to_excel('testbook.xlsx')
There are multiple ways of doing it if you want to do it completely using pandas library this will work.

Using pandas to replace data in excel sheet

I tried to come up with a way to copy data from a sheet in an excel file as
import pandas as pd
origionalFile = pd.ExcelFile('AnnualReport-V5.0.xlsx')
Transfers = pd.read_excel(origionalFile, 'Sheet1')
I have another excel file, which named 'AnnualReport-V6.0.xlsx', it has existing data in the sheet named 'Transfers', I tried to use the dataframe I created easily on to replace data in the sheet 'Transfers' in 'AnnualReport-V6.0.xlsx' from column B, leave column A as it is.
I did a few searches, the closest to what I want is this
Modifying an excel sheet in a excel book with pandas
but it does not allow me the keep column A in the original sheet (column A has some equations I do want to keep them), any idea how to do it? Thanks
Would reading column A and inserting it to the fresh data you want to write solve your problem?

How to add a dataframe to an existing Excel sheet with Pandas, on a .xlsm file

I want to import the values from a Pandas dataframe into an existing Excel sheet. I want to insert the data inside the sheet without deleting what is already there in the other cells (like formulas using those datas etc).
I tried using data.to_excel like:
writer = pd.ExcelWriter(r'path\TestBook.xlsm')
data.to_excel(writer, 'Sheet1', startrow=1, startcol=11, index = False)
writer.save()
The problem is that this way i overwrite the entire sheet.
Is there a way to only add the dataframe? It would be perfect if I could also keep the format of the destination cells.
Thanks
I found a good solution for it. Xlwings natuarally supports pandas dataframe:
https://docs.xlwings.org/en/stable/datastructures.html#pandas-dataframes
The to_excel function provides a mode parameter to insert (w) of append (a) a data frame into an excel sheet, see below example:
with pd.ExcelWriter(p_file_name, mode='a') as writer:
df.to_excel(writer, sheet_name='Data', startrow=2, startcol=2)

Append a pandas dataframe to an existing excel table

I need some help with the following.
I currently use python pandas to open a massive spreadsheet every day (this spreadsheet is a report, hence every day the data inside the spreadsheet is different). Pandas dataframe allows me to quickly crunch the data and generate a final output table, with much less data than the initial excel file.
Now, on day 1, I would need to add this output dataframe (3 rows 10 columns) to a new excel sheet (let's say sheet 1).
On day 2, I would need to take the new output of the dataframe and append it to the existing sheet 1. So at the end of day 2, the table in sheet1 would have 6 rows and 10 columns.
On day 3, same thing. I will launch my python pnadas tool, read data from the excel report, generate an output dataframe 3x10 and append it again to my excel file.
I can't find a way to append to an existing excel table.
Could anybody help?
Many thanks in advance,
Andrea
If you use openpyxl's utilities for dataframes then you should be able to do everything you need with the existing workbook, assuming this fits into memory.
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
wb = load_workbook("C:\Andrea\master_file.xlsx")
ws = wb[SHEETNAME]
for row in dataframe_to_rows(dt_today):
ws.append(row)

Categories

Resources