简体   繁体   English

openpyxl dataframe_to_rows 到现有工作表上

[英]openpyxl dataframe_to_rows onto existing sheet

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?是否可以使用 openpyxl 将 pandas dataframe 写入现有工作表,还是只能通过创建新的工作簿和工作表? 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")

There was some excel formatting in the existing sheets, after deleting them and recreating them this method worked.现有工作表中有一些 excel 格式,在删除它们并重新创建它们后,这种方法有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM