用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - python代码库

读取写入excel例子2

2020-02-07 作者:云代码会员举报

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


分享到:
更多

网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。