python中修改execl、xlrd、xlutils

import xlrd
from xlutils import copy

book = xlrd.open_workbook('students.xls')
sheet0 = book.sheet_by_index(0) # 获取原来的数据

new_book = copy.copy(book)
sheet = new_book.get_sheet(0) # 这个是新的sheet页
sheet.write(0, 6, '年龄阶段') # 写表头

for i in range(1, sheet0.nrows):
# age = sheet0.row_values(i)[3]
age = sheet0.cell(i, 3).value
if age < 18:
word = '未成年'
elif age >= 18 and age <= 30:
word = '青年'
else:
word = '中年人'
sheet.write(i, 6, word)

new_book.save('students.xls')

# 1、把每个学生的年龄阶段,加入到excel

# 1、表头加上,年龄阶段
# 2、先取到每行数据里面的年龄,判断属于哪个年龄阶段的


# 1、用xlrd模块打开excel
# 2、用xlutils模块里面的copy复制一份
# 3、获取到sheet页
# 4、修改

posted on 2019-05-13 21:12  dongxl  阅读(649)  评论(0)    收藏  举报

导航