Write Dataframe to excel with template - python

I am trying to write my dataframe to excel. I am able to write the data using pandas.
df.to_excel(r'Path where the exported excel file will be stored\File Name.xlsx', index = False)
But the excel I am trying to write contain some template which look something like this.
Whenever I try to write the df values to excel using df.to_excel it always remove the template and write is there way I can write the data below the template in excel.
Any suggestions?

I am able to solve this using below code:
import pandas as pd
from openpyxl import load_workbook
path = "Excel.xlsx"
book = load_workbook(path)
writer = pd.ExcelWriter("Excel.xlsx", engine='openpyxl')
writer.book = book
writer.sheets = {ws.title: ws for ws in book.worksheets}
df.to_excel(writer, startrow=writer.sheets['Sheet1'].max_row, index=False, header=False)
writer.save()

Related

add new sheet with data validation to existing excel workbook using python

i have an excel workbook that i want to add new sheets to using dataframes created in python without losing the existing data in the workbook
i want these new sheets to contain data validation columns in excel such that it will only allow certain values in a list to be input
pls help this keeps overwriting my work
thanks in advance!
# writing first df to the file
path_to_file = '/master_file.xslx'
raw_data_df.to_excel(path_to_file, sheet_name='raw_data', index=False)
# writing second df to same file in a different sheet
# this sheet should have data validation
items = list(range(1,10))
writer = pd.ExcelWriter(path_to_file, engine='xlsxwriter')
df2.to_excel(writer, index=False, sheet_name="sheet_2", startrow=0)
wb = writer.book
ws = writer.sheets["sheet_2"]
ws.data_validation(excel_cells, {"validate": "list", "source": items})
writer.save()
This keeps overwriting my original sheet or saying that the file is corrupted

Python: Copy sheets from excel workbook and paste into new workbook

I'm a super beginner and still learning Python.
I have an excel workbook which contains multiple sheets and only want certain sheets to be copied and pasted in a new created worbook and Im having some troubles.
below is my code.
import pandas as pd
import openpyxl
df = pd.read_excel('AMT.xlsb', sheet_name=['Roster','LOA'])
# print whole sheet data
with pd.ExcelWriter('output.xlsx') as writer:
df.to_excel(writer, sheet_name=['Roster','LOA'])
I get an error "IndexError: At least one sheet must be visible", none of the sheets from the AMT file are hidden.
Looks like you may be converting your frame to a dict - Try this:
import pandas as pd
import openpyxl
df = pd.read_excel('AMT.xlsb', sheet_name='Roster')
df1 = pd.read_excel('AMT.xlsb', sheet_name='LOA')
# print whole sheet data
with pd.ExcelWriter('output.xlsx') as writer:
df.to_excel(writer, sheet_name="Roster", index=False)
df1.to_excel(writer, sheet_name="LOA", index=False)
You may still have some clean up after...

Pandas Excel Writer append mode is not working

I created a pandas dataframe in my code and tried to append the final output to an existing Excel workbook. The existing workbook is called "Directory" and has three different sheets in it. I want to append my output to one of the sheets called "raw_data in the workbook". This sheet already has some data in it but the columns in this sheet match the columns in my new dataframe. Here is my code:
from pandas import ExcelWriter
from pandas import ExcelFile
from openpyxl import Workbook
with pd.ExcelWriter(r'C:\Users\Documents\Directory.xlsx', engine ='openpyxl', mode='a') as writer:
df.to_excel(writer, sheet_name = 'raw_data', index = False, header = False)
writer.save()
writer.close()
My code "runs" without any error but when I check the workbook after running the code, my code doesn't append my data frame to the specified sheet, "raw_data", but creates a new sheets called "raw_data1" and store the data in that tab. I couldn't figure out which part in my code is incorrect. Could anyone please help me with this? Thank you.

Pandas how to keep sheets untouched

I have a excel workbook that has more than one worksheets (i.e. sheet1 and sheet2)
and i did like this:
import pandas
df1 = pandas.read_excel('file.xlsx', sheet_name='sheet1')
####doing something on shee1, sheet2 is not touched######
df1.to_excel('file.xlsx', sheet_name='sheet1')
By doing above, I found sheet2 missing after saving the file.
Is there a way to open and save on same file without affecting other worksheets?
A possible way to do that is by loading all of your sheets, then modifying only the first one. Although it works, you may loose any custom styling from your tables.
# Load all sheets
workbook = pd.read_excel('file.xlsx', sheet_name=None)
# do something to workbook['sheet1']
# Write all sheets to excel file
writer = pd.ExcelWriter('file.xlsx', engine='xlsxwriter')
for sheet, df in workbook.items():
df.to_excel(writer, sheet_name=sheet)
writer.save()
As far as I know, the only way to overwrite a sheet ─ while keeping the other ones untouched ─ requires using third-party libraries. For instance,
here's an option with openpyxl:
First, modify the data as you wish:
import pandas as pd
fname = 'file.xlsx'
target_sheet = 'sheet1'
df = pd.read_excel('file.xlsx', sheet_name='sheet1')
# further modification to `df` ...
then, save it to the specified sheet:
# Load required functions
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
# Read excel file (all sheets)
wb = load_workbook(fname)
# Get the index from target-sheet
idx = wb.sheetnames.index(target_sheet)
# Delete the existing target-sheet
del wb[target_sheet]
# Create a new empty target-sheet
wb.create_sheet(target_sheet, idx)
# Write `df` data on it
for r in dataframe_to_rows(df, index=False, header=True):
wb[target_sheet].append(r)
# Save file
wb.save(fname)
I suspect something is going on in the below section that is throwing off the code:
####doing something on ws1, ws2 is not touched######
When I ran your code on my system the workbook still returned both worksheets
As an isolation test can you comment/remove the code in that section and confirm if the error still appears.

Pandas to excel: append a dataframe to an excel sheet with existing data w/o overwritting

This question has been asked but I tried every solution and it still overwrite what I already have in my sheet. What I have tried:
writer = pd.ExcelWriter('/home/viktor/Downloads/kpi_report_current.xlsx', engine='openpyxl')
df_kpi.to_excel(writer, index=True)
writer.save()
But it overwrite the dataframe that I previously exported to the excel sheet...I tried every solution on the following page:How to write to an existing excel file without overwriting data (using pandas)?
To be more precise, I tried the following:
from openpyxl import load_workbook
book = load_workbook('/home/viktor/Downloads/kpi_report_current.xlsx')
writer = pd.ExcelWriter('/home/viktor/Downloads/kpi_report_current.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df_kpi.to_excel(writer)
writer.save()
Which load the existing data on the excel sheet, but it is still overwritting it...
Any other idea? Thanks!

Categories

Resources