[python]代码库
# 处理人员信息2,将同一类数据合并
import xlrd, xlwt
from xlutils.copy import copy
people = []
def start():
data = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\test.xlsx")
newb = copy(data)
table = data.sheet_by_name(r"Sheet1")
title = table.row_values(1)
nrows = table.nrows
for h in range(2, nrows):
values = table.row_values(h)
people.append(dict(zip(title, values)))
worksheet = newb.add_sheet('修改后')
style = xlwt.XFStyle() # 初始化样式
font1 = xlwt.Font() # 为样式创建字体
font1.name = '宋体'
font1.height = 20 * 11
font1.bold = False
style.font = font1
worksheet.write(1, 0, '姓名', style) # 带样式的写入
worksheet.write(1, 1, '数据', style)
worksheet.write(1, 2, '电话', style)
wl = 2
name_t = people[0]['姓名']
data_t = people[0]['数据']
phone_t=people[0]['电话']
for peop in people[1:]:
if peop['姓名'] == name_t:
data_t = data_t + "、" + peop['数据']
else:
worksheet.write(wl, 0, name_t, style)
worksheet.write(wl, 1, data_t, style)
worksheet.write(wl, 2, phone_t, style)
wl=wl+1
name_t=peop['姓名']
data_t=peop['数据']
phone_t=peop['电话']
worksheet.write(wl, 0, name_t, style)
worksheet.write(wl, 1, data_t, style)
worksheet.write(wl, 2, phone_t, style)
newb.save(r'C:\Users\Administrator\Desktop\test.xlsx')
if __name__ == '__main__':
start()