0

I have a time series made of: dates, price1, price2. with lets say 392 observations, stored in a dataframe and previously streamed from an Api. I would like to add these rows to an existent xlsx file using openpyxl. However I don't know how to add these values to the xlsx. Can you help me?

y = df.sort_index(x, ascending=False) # sorted newest first
count = len(y)
print(count)

from openpyxl import load_workbook

wb = load_workbook(filename = 'Example.xlsx')
ws = wb['cc']
ws.insert_rows(1,count)

for row in range(2, count):
    for col in range(1, 3):
        _ = ws.cell(column=col, row=row, value=y)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • What seems to be the problem? An actual error or what's not working as expected could be helpful. Anyway it seems as `y` is a DataFrame itself and you're to write that to a single cell... – Tomerikoo Jul 02 '19 at 18:38
  • 1
    Did you call wb.save() after your for loop to write the cells? https://openpyxl.readthedocs.io/en/stable/tutorial.html#saving-to-a-file Or do you need help indexing/walking through the dataframe `y`? – mgrollins Jul 02 '19 at 18:42
  • Duplicate of the following: https://stackoverflow.com/questions/38074678/append-existing-excel-sheet-with-new-dataframe-using-python-pandas https://stackoverflow.com/questions/20219254/how-to-write-to-an-existing-excel-file-without-overwriting-data-using-pandas/47740262#47740262 – SBM Jul 02 '19 at 18:43
  • `dataframe_to_rows()` is what you need. – Charlie Clark Jul 03 '19 at 08:10

0 Answers0