Skip to content

Commit

Permalink
fix: Modify 'load' behavior to append data to existing sheet
Browse files Browse the repository at this point in the history
Details:
- Added logic to detect existing sheet and append data if present.
- There is no direct way to append on the sheet, so I am reading the sheet to get the last row number and then writing after that row number.

Resolves: #5472
  • Loading branch information
ssahaxd committed Oct 1, 2023
1 parent 020b1ac commit 156af94
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions openbb_terminal/helper_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1602,15 +1602,28 @@ def export_data(
)

elif saved_path.exists():
# Load the Excel file to get the existing data
existing_df = pd.read_excel(saved_path, sheet_name=sheet_name)
# Get the number of rows in the existing data
start_row = (
existing_df.shape[0] + 1
) # Add 1 to start writing after the last row

# Append data to the existing sheet
with pd.ExcelWriter(
saved_path,
mode="a",
if_sheet_exists="new",
if_sheet_exists="overlay",
engine="openpyxl",
) as writer:
df.to_excel(
writer, sheet_name=sheet_name, index=True, header=True
writer,
sheet_name=sheet_name,
startrow=start_row,
index=True,
header=False,
)

else:
with pd.ExcelWriter(
saved_path,
Expand Down

0 comments on commit 156af94

Please sign in to comment.