openpyxl dataframe_to_rows onto existing sheet

Viewed 1028

Is it possible to write a pandas dataframe to an existing sheet with openpyxl or is it only possible with creating a new workbook and sheet? There is nothing being written when I try to write into the existing sheet.

import pandas as pd
import openpyxl
from openpyxl.utils.dataframe import dataframe_to_rows


mydf = pd.read_excel("./xlsx/df1.xlsx", sheet_name="Cars")

template = openpyxl.load_workbook("./templates/Template.xlsx")

for r in dataframe_to_rows(mydf, index=False, header=True):
    template['Cars'].append(r)

template.save("mynewexcelfile.xlsx")

1 Answers

There was some excel formatting in the existing sheets, after deleting them and recreating them this method worked.

Related