0

I'm trying to download a file>copy the data into an Excel file then loop this for 12 downloads. All pasted below each other into the same spreadsheet. Below is my code and it seems to paste the data over the other even though I am changing the starting row each loop. What am I missing?

fn = pd.ExcelWriter(r'file.xlsx')
lst = [3, 6, 9]

df.to_excel(fn, sheet_name='Sheet1', header=None, index=False,
             startcol = 0,startrow = 0)

for _ in range(len(lst)):
    browser.switch_to.window(browser.window_handles[0])
    time.sleep(3)
    clear = browser.find_element_by_xpath("/html/body/form/table/tbody/tr[3]/td/table[2]/tbody/tr/td/div[2]/div[2]/table/tbody/tr[2]/td/div/table/tbody/tr[2]/td/div/div[2]/div[3]/div[3]/table/tbody/tr/td[2]/input[4]")
    clear.click()
    nexthotel = browser.find_element_by_xpath("/html/body/form/table/tbody/tr[3]/td/table[2]/tbody/tr/td/div[2]/div[2]/table/tbody/tr[2]/td/div/table/tbody/tr[2]/td/div/div[2]/div[3]/div[3]/table/tbody/tr/td[1]/select/option[1]")
    nexthotel.click()
    move = browser.find_element_by_xpath("/html/body/form/table/tbody/tr[3]/td/table[2]/tbody/tr/td/div[2]/div[2]/table/tbody/tr[2]/td/div/table/tbody/tr[2]/td/div/div[2]/div[3]/div[3]/table/tbody/tr/td[2]/input[2]")
    move.click()
    submit = browser.find_element_by_xpath("/html/body/form/table/tbody/tr[3]/td/table[2]/tbody/tr/td/div[2]/div[2]/table/tbody/tr[2]/td/div/table/tbody/tr[2]/td/div/div[2]/div[3]/table/tbody/tr/td/input")
    submit.click()
    time.sleep(5)
    paths = sorted(Path(r'downloads').iterdir(), key=os.path.getmtime)
    len(paths)
    df = (paths[len(paths)-1])
    df = pd.read_excel(df)
    a = ['MTD WD May 2020', 'MTD WE May 2020', 'Period']
    try:
        df = df[df['Daily by Day Performance Data'].isin(a)]
    except KeyError:
        df = df[df['Daily by Day Performance Data - Preliminary'].isin(a)]

    from openpyxl import load_workbook

    fn = pd.ExcelWriter(file.xlsx')

    df.to_excel(fn, sheet_name='Sheet1', header=None, index=False,
             startcol = 0,startrow = (lst[_]))
    browser.switch_to.window(browser.window_handles[0])
    time.sleep(5)
    fn.save()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Does this answer your question: https://stackoverflow.com/questions/38074678/append-existing-excel-sheet-with-new-dataframe-using-python-pandas ? – Serge de Gosson de Varennes Nov 19 '20 at 08:47
  • 3
    Does this answer your question? [Append existing excel sheet with new dataframe using python pandas](https://stackoverflow.com/questions/38074678/append-existing-excel-sheet-with-new-dataframe-using-python-pandas) – Serge de Gosson de Varennes Nov 19 '20 at 08:47

0 Answers0