'''
明明导入xlutils库,为什么还提示copy未定义
Traceback (most recent call last):
File "D:/python学习/Chapter-3-17-if语句进行xls数据处理.py", line 9, in <module>
nwb = copy(read_book)
NameError: name 'copy' is not defined
先导入库,读取工作簿,读取工作表,读取对应的列,做逻辑,导入
'''
import xlrd
import xlutils
read_book = xlrd.open_workbook('工资表.xls')
read_sheet = read_book.sheet_by_name('工作表')
nwb = copy(read_book)
nws = nwb.get_sheet('工作表')
read_lie = read_sheet.col_values(1)
hang_num = 0
for n in read_lie:
if type(n) == float:
if n > 180:
nws.write(hang_num, 2, '真有钱!')
elif n > 150:
nws.write(hang_num, 2, '有点前嘛!')
elif n > 100:
nws.write(hang_num, 2, '普通打工族!')
elif n > 80:
nws.write(hang_num, 2, '生活苦难!')
else:
nws.write(hang_num, 2, '加油啊!')
hang_num += 1
nwb.save('Chapter-3-17形成的表格.xls')
如果你要直接调用copy方法的话导入需要这么写
from xlutils.copy import copy