0

I am trying to update an excel sheet with Python codes. I read specific cell and update it accordingly but Padadas overwrites the entire excelsheet which I loss other pages as well as formatting. Anyone can tell me how I can avoid it?

       Record = pd.read_excel("Myfile.xlsx", sheet_name'Sheet1', index_col=False)
       Record.loc[1, 'WORDS'] = int(self.New_Word_box.get())
       Record.loc[1, 'STATUS'] = self.Stat.get()
       Record.to_excel("Myfile.xlsx", sheet_name='Student_Data', index =False)

My code are above, as you can see, I only want to update few cells but it overwrites the entire excel file. I tried to search for answer but couldn't find any specific answer.

Appreciate your help.

user3064089
  • 65
  • 1
  • 3
  • 10

1 Answers1

2

Update: Added more clarifications

Steps:

1) Read the sheet which needs changes in a dataframe and make changes in that dataframe.

2) Now the changes are reflected in the dataframe but not in the sheet. Use the following function with the dataframe in step 1 and name of the sheet to be modified. You will use the truncate_sheet param to completely replace the sheet of concern. The function call would be like so:

append_df_to_excel(filename, df, sheet_name, startrow=0, truncate_sheet=True)
from openpyxl import load_workbook
import pandas as pd

def append_df_to_excel(filename, df, sheet_name="Sheet1", startrow=None,
                       truncate_sheet=False,
                       **to_excel_kwargs):
    """
    Append a DataFrame [df] to existing Excel file [filename]
    into [sheet_name] Sheet.
    If [filename] doesn"t exist, then this function will create it.

    Parameters:
      filename : File path or existing ExcelWriter
                 (Example: "/path/to/file.xlsx")
      df : dataframe to save to workbook
      sheet_name : Name of sheet which will contain DataFrame.
                   (default: "Sheet1")
      startrow : upper left cell row to dump data frame.
                 Per default (startrow=None) calculate the last row
                 in the existing DF and write to the next row...
      truncate_sheet : truncate (remove and recreate) [sheet_name]
                       before writing DataFrame to Excel file
      to_excel_kwargs : arguments which will be passed to `DataFrame.to_excel()`
                        [can be dictionary]

    Returns: None
    """
    # ignore [engine] parameter if it was passed
    if "engine" in to_excel_kwargs:
        to_excel_kwargs.pop("engine")

    writer = pd.ExcelWriter(filename, engine="openpyxl")

    # Python 2.x: define [FileNotFoundError] exception if it doesn"t exist
    try:
        FileNotFoundError
    except NameError:
        FileNotFoundError = IOError

    if "index" not in to_excel_kwargs:
        to_excel_kwargs["index"] = False

    try:
        # try to open an existing workbook
        if "header" not in to_excel_kwargs:
            to_excel_kwargs["header"] = True
        writer.book = load_workbook(filename)

        # get the last row in the existing Excel sheet
        # if it was not specified explicitly
        if startrow is None and sheet_name in writer.book.sheetnames:
            startrow = writer.book[sheet_name].max_row
            to_excel_kwargs["header"] = False

        # truncate sheet
        if truncate_sheet and sheet_name in writer.book.sheetnames:
            # index of [sheet_name] sheet
            idx = writer.book.sheetnames.index(sheet_name)
            # remove [sheet_name]
            writer.book.remove(writer.book.worksheets[idx])
            # create an empty sheet [sheet_name] using old index
            writer.book.create_sheet(sheet_name, idx)

        # copy existing sheets
        writer.sheets = {ws.title: ws for ws in writer.book.worksheets}
    except FileNotFoundError:
        # file does not exist yet, we will create it
        to_excel_kwargs["header"] = True

    if startrow is None:
        startrow = 0

    # write out the new sheet
    df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs)

    # save the workbook
    writer.save()

We can't replace openpyxl engine here to write excel files as asked in comment. Refer reference 2.

References:

1) https://stackoverflow.com/a/38075046/6741053

2) xlsxwriter: is there a way to open an existing worksheet in my workbook?

รยקคгรђשค
  • 1,919
  • 1
  • 10
  • 18
  • Thanks but it is using openpyxl which is different to Pandas. I wan to use Pandas only. Any chance I can use the Pandas only? – user3064089 Feb 24 '20 at 09:01
  • I have to admit I am lost here. There are mixed of openpyxl and Pandas. For example .save() is not pandas function and if I used the df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs), I will get the same problem of lossing my data. – user3064089 Feb 24 '20 at 09:10
  • .save() is excel writer's function to save the workbook, you don't need to df.to_excel instead send the df to above function. – รยקคгรђשค Feb 24 '20 at 09:18
  • Thanks again, but can you elaborate on your statement ".save() is excel writer's function to save the workbook, you don't need to df.to_excel instead send the df to above function" ? if I dont need the df.to_excel and .save() is not pandas function, how can I save my data? – user3064089 Feb 24 '20 at 09:27
  • The function will do that for you, it will perform those actions so that your data is not lost and only make changes in the sheetname you provided. The data of rest of the sheets will remain intact. I will add more clarifications in a while. – รยקคгรђשค Feb 24 '20 at 09:40
  • @user3064089 Added clarifications in the answer. Does this answer your doubts? – รยקคгรђשค Feb 24 '20 at 10:12
  • Thanks again. I have created a dataframe called Record. I make the changes in that data frame in my code line 2-3 and for saving, do I need to use the append_df_to_excel() function? I am sorry if I have a bit lost.... – user3064089 Feb 24 '20 at 12:02
  • yes in the next step, you only need to call the function with your dataframe with parameters as specified in the answer. You don't need to be sorry everytime, a doubt is a doubt. – รยקคгรђשค Feb 24 '20 at 12:08
  • Hi @user3064089, did my answer help you? If it did, kindly accept the answer or if you have any doubts or you are stuck at something feel free to ask them out. – รยקคгรђשค Mar 03 '20 at 02:29
  • Hi Suparshva, many thanks for your guidance. At the end I ditched using Pandas and went a head to use openpyxl.... – user3064089 Mar 05 '20 at 09:50
  • @user3064089 you can use both together as well as I use in my answer... openpyxl is just an excelwriter engine. If something worked for you, you can answer your own question and accept that too. It might help future users who face similar issue. – รยקคгรђשค Mar 05 '20 at 10:11