首页 > 编程语言 > 详细

Python xlutils excel追加数据

时间:2021-09-07 15:45:23      阅读:17      评论:0      收藏:0      [点我收藏+]

代码示例:

# -*-encoding: utf-8 -*-

# 打开目标excel
# 获取excel中第一个表单
# 读取出里面写入的行数
# 将xlrd 的 workbook 转为 xlwt的 workbook 对象
# 追加数据
import xlrd3
import xlwt
from xlutils.copy import copy

info = [
    [‘张三疯‘, 27.0, ‘打游戏‘],
    [‘陈二狗‘, 25.0, ‘打自己‘],
    [‘程小鸭‘, 26.0, ‘打人‘],
    [‘贾精忠‘, 27.0, ‘被人打‘]
]
# 打开目标excel
targetExcel = xlrd3.open_workbook(‘aoe.xlsx‘)
# 获取excel中表单数
sheetNames = targetExcel.sheet_names()
# 获取excel中第一个表单
first_sheet = targetExcel.sheet_by_name(sheetNames[0])
# 读取出excel 里面的有效行 和 有效列
nrows = first_sheet.nrows
ncols = first_sheet.ncols

# 将xlrd 的 workbook 转为 xlwt的 workbook 对象

workBook = copy(targetExcel)

newSheet = workBook.get_sheet(0)

# 追加数据
i = nrows

for item in info:
    for j in range(ncols):
        newSheet.write(i,j,item[j])
    i = i + 1

# 保存
workBook.save(‘aoe.xlsx‘)

图例:

追加前:

技术分享图片

 

 追加后:

技术分享图片

 

Python xlutils excel追加数据

原文:https://www.cnblogs.com/Avicii2018/p/15237667.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!