D
D
Danil Samodurov2020-09-07 11:48:55
Python
Danil Samodurov, 2020-09-07 11:48:55

How to add an entry to an excel file without overwriting it?

Hello. I process the data with a python script:

import os
import pandas as pd

data = pd.read_excel("ОБЩИЙ ЖУРНАЛ.xls")
for i in data:
    if "Unnamed" in i:
        del data[i]
    else:
        continue

for i in data["Протокол, №"]:
    data_protocol = data[data["Протокол, №"] == i]
    graphs_dir = os.path.join(os.getcwd(), "Graphs")
    writer = pd.ExcelWriter(f'{os.path.join(graphs_dir, f"{i}.xlsx")}', engine='xlsxwriter', mode='w')
    try:
        os.mkdir(graphs_dir)
    except OSError:
        pass
    for j in data_protocol["T, °C"]:
        data_temper = data_protocol[data_protocol["T, °C"] == j]
        for k in data_temper["R"]:
            data_R = data_temper[data_temper["R"] == k]
            data_R.to_excel(writer, sheet_name=f'{i}_T={j}°C_R={k}')
    else:
        writer.save()


The script was written a long time ago. As a result, I created a number of excel files. Now you need to process the data again and add it to the previously created files. The documentation for pandas.ExcelWriter says that you can change mode='w' to mode='a'. But after launch, an error occurs:
ValueError: Append mode is not supported with xlsxwriter!
I tried to remove the engine='xlsxwriter' parameter, but it is the default and the same error crashes.
Does anyone have any idea?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
ScriptKiddo, 2020-09-07
@samodurOFF

Pandas can append to files with the "openpyxl" engine.
If openpyxl is not installed, install it before running the script

writer = pd.ExcelWriter(f'{os.path.join(graphs_dir, f"{i}.xlsx")}', engine='openpyxl', mode='a')

UPD. If you need to add data to an already existing sheet - here is a snippet from Stackoverflow
https://stackoverflow.com/a/38075046
Code

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
    """
    from openpyxl import load_workbook

    import pandas as pd

    # 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


    try:
        # try to open an existing workbook
        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

        # 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
        pass

    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()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question