python写入excel表格,Python如何使用ExcelWriter写入现有工作表

本文介绍了如何使用Python的pandas库和openpyxl库,通过ExcelWriter将数据写入或追加到已存在的Excel工作簿的不同工作表中。问题在于,直接调用`Out_Excel`函数会覆盖原有的工作簿内容。解决方案是先使用`load_workbook`加载现有的Excel文件,然后将工作簿与ExcelWriter关联,并逐个工作表写入数据。
摘要由CSDN通过智能技术生成

I am trying to use ExcelWriter to write/add some information into a workbook that contains multiple sheets.

First time when I use the function, I am creating the workbook with some data. In the second call, I would like to add some information into the workbook in different locations into all sheets.

def Out_Excel(file_name,C,col):

writer = pd.ExcelWriter(file_name,engine='xlsxwriter')

for tab in tabs: # tabs here is provided from a different function that I did not write here to keep it simple and clean

df = DataFrame(C) # the data is different for different sheets but I keep it simple in this case

df.to_excel(writer,sheet_name = tab, startcol = 0 + col, startrow = 0)

writer.save()

In the main code I call this function twice with different col to print out my data in different locations.

Ou

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值