python向已存在的excel中新增表,不覆盖原数据的实例

作者:xxzhix 时间:2022-04-26 15:16:51 

每月需更新某个excel表格,进行两项操作,且不覆盖原有的sheet:

1. 在原来的excel表中新增sheet

2. 往原有的excel表中的某张sheet新增内容

基于python3,使用xlrd,xlwt,具体代码如下,亲测有效,希望对大家有帮助,谢谢!


import xlwt
import xlrd
from xlutils.copy import copy
#打开需要操作的excel表
wb=xlrd.open_workbook(path)
#复制原有表
newb=copy(wb)
#新增sheet,参数是该sheet的名字,可自定义
wbsheet=newb.add_sheet(dl+'-'+dn)
#向新sheet中写入数据。本代码中的d是某个dataframe
wbsheet.write(0,0,'date')
wbsheet.write(0,1,'visited')
wbsheet.write(0,2,'success')
for i in range(d.shape[0]):
 wbsheet.write(i + 1, 0, d.iloc[i, 0])
 for j in range(1,d.shape[1]):
   wbsheet.write(i+1,j,int(d.iloc[i,j]))
#获取原有excel表中sheet名为‘summary'的sheet
sumsheet=newb.get_sheet('summary')
#k表示该sheet的最后一行
k=len(sumsheet.rows)
#想原有sheet后面新增数据
sumsheet.write(k,0,dl+'-'+dn)
sumsheet.write(k,1,int(sum(d['visited'])))
sumsheet.write(k,2,int(sum(d['success'])))
#保存为原有的excel表路径
newb.save(path)

来源:https://blog.csdn.net/xxzhix/article/details/79324554

标签:python,excel,新增,表,覆盖
0
投稿

猜你喜欢

  • PHP实现的获取文件mimes类型工具类示例

    2023-10-07 09:33:33
  • css学习笔记: 重置默认样式 css reset

    2009-07-19 14:30:00
  • python matplotlib绘图,修改坐标轴刻度为文字的实例

    2023-09-29 12:27:57
  • 使用python实现数组、链表、队列、栈的方法

    2021-01-03 22:40:20
  • python获取磁盘号下盘符步骤详解

    2022-10-05 08:32:32
  • 在SQL Server中使用CLR调用.NET方法

    2008-12-24 15:43:00
  • javascript验证IP地址等验证例子

    2007-09-11 13:40:00
  • 如何设置SQL Server数据库全文索引服务

    2009-01-13 13:46:00
  • 正则的replace方法(产生的字符串副本)

    2008-06-03 13:31:00
  • Python实现曲线拟合操作示例【基于numpy,scipy,matplotlib库】

    2023-07-19 23:31:35
  • Python实现的各种常见分布算法示例

    2021-12-06 18:26:15
  • Python第三方包之DingDingBot钉钉机器人

    2022-09-06 14:50:38
  • javascript闭包的秘密

    2008-09-28 20:39:00
  • 在asp中使用存储过程

    2008-02-26 12:17:00
  • python实现简易的学生信息管理系统

    2021-08-24 12:07:09
  • po+selenium+unittest自动化测试项目实战

    2022-05-03 14:46:46
  • 详解前端自动化工具gulp自动添加版本号

    2023-08-09 14:48:41
  • asp 网站静态化函数代码html

    2011-03-16 11:21:00
  • ASP实现GB2312转UTF-8函数

    2009-02-26 13:08:00
  • python实现贪吃蛇双人大战

    2022-10-10 19:18:37
  • asp之家 网络编程 m.aspxhome.com