python读取写入excel的库_读取写入excel例子2

[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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值