I'm trying to append some information to an excel using pandas.
My excel has several sheets, most of them with formulas.
I'm only trying to replace the cells from a specific sheet sheet2.
Function:
def write():
df = pd.read_excel(path, "sheet2")
df.loc['A','B'] = 10
with pd.ExcelWriter(path, engine="openpyxl", mode="a", if_sheet_exists="replace") as writer:
df.to_excel(writer, sheet_name="sheet2")
The problem
The sheet cell values get replaced BUT in other sheets the cells with formulas are empty.
When opening the excel itself in protected view they are empty, but when editing they reappear.
Help.
Related
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
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.
I need to write multiple dataframes to an excel file. These dataframes needs to be written to a specific sheet and it should not overwrite existing data on that sheet.
The code I have is as follows:
excelbook = test.xlsx
book = load_workbook(excelbook)
writer = pd.ExcelWriter(excelbook, engine = 'openpyxl')
writer.book = book
df.to_excel(writer, sheet_name = 'apple', startcol=5, startrow=0)
writer.save()
writer.close()
Problem with my code is, each time I run it to write a dataframe, it is creating a new sheet in the excel file. For example, if the sheet name I need is "apple", then since I'm running this piece of code 3 times (to write 3 dataframes to the same sheet), it is creating a new sheet each time and naming them as - "apple1", "apple2" and "apple3"
I need to write multiple dataframes to the same excel file, to the same sheet in that file, without overwriting the existing data in the sheet.
Please help. Thanks in advance.
I just want to overwrite certain column base on my dataframe. Suppose df2 is my dataframe.
Below is the code i use. The problem is its overwrite the other columns and row even though i code it to start on columns 80.
I want its overwrite on column 80 and beyond only, but not before the column 80. 80 is index, not name.
import pandas as pd
import xlsxwriter
df2 = pd.read_excel(r'C:\Users\RUI LEONHART\Google Drive\Shop\STOCK V2.xlsx',
usecols=['XS1','S1','M1','L1','XL1','XXL1'])
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df2.to_excel(writer, sheet_name='Sheet1', startcol=80)
# Get the xlsxwriter objects from the dataframe writer object.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Close the Pandas Excel writer and output the Excel file.
writer.save()
I search around the solution. The closest one is this
python: update dataframe to existing excel sheet without overwriting contents on the same sheet and other sheets
but still overwrite the columns and row that i dont want.
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)