分享

读取写入excel例子

 文炳春秋 2020-04-02

[python]代码库

# 处理人员信息

import xlrd, xlwt

people = []


def start():
    data = xlrd.open_workbook(r"D:/people.xlsx")
    table = data.sheet_by_name(r"Sheet1")
    title = table.row_values(0)
    nrows = table.nrows
    for in range(1, nrows):
        values = table.row_values(h)
        people.append(dict(zip(title, values)))


def t1():
    workbook = xlwt.Workbook(encoding='utf-8')
    worksheet = workbook.add_sheet('My Worksheet')
    style = xlwt.XFStyle()  # 初始化样式
    font1 = xlwt.Font()  # 为样式创建字体
    font1.name = '黑体'
    font1.height = 20 * 20
    font1.bold = True
    font2 = xlwt.Font()  # 为样式创建字体
    font2.name = '宋体'
    font2.height = 20 * 11
    font2.bold = False
    alignment = xlwt.Alignment()  # 设置单元格对齐方式
    alignment.horz = 0x02  # 0x01(左端对齐)、0x02(水平方向上居中对齐)、0x03(右端对齐)
    alignment.vert = 0x01  # 0x00(上端对齐)、 0x01(垂直方向上居中对齐)、0x02(底端对齐)

    worksheet.col(0).width = 256 * 40
    worksheet.col(1).width = 256 * 12
    worksheet.col(2).width = 256 * 16
    worksheet.col(3).width = 256 * 16
    worksheet.col(4).width = 256 * 16

    style.font = font1
    style.alignment = alignment
    worksheet.write_merge(0004'编制人员报表', style)  # 带格式合并单元格写入,0行0列到0行4列合并
    style.font = font2
    worksheet.write(10'单位', style)  # 带样式的写入
    worksheet.write(11'姓名', style)
    worksheet.write(12'职务', style)
    worksheet.write(13'工龄', style)
    worksheet.write(14'本年度月份', style)

    wl=2
    for peop in people:
        if peop['编制类别']=='全额拨款':
            worksheet.write(wl,0,peop['主管部门'],style)
            worksheet.write(wl, 1, peop['姓名'], style)
            worksheet.write(wl, 2, peop['领导职务层次'], style)
            worksheet.write(wl, 3, peop['参加工作时间'], style)
            worksheet.write(wl, 4, peop['进入本单位时间'], style)
            wl=wl+1

    workbook.save(r'D:/test.xls')  # 保存文件


if __name__ == '__main__':
    start()
    print(people)
    # t1()
# 处理人员信息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 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(10'姓名', style)  # 带样式的写入
    worksheet.write(11'数据', style)
    worksheet.write(12'电话', 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()

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约