• python操作Excel-写/改/读


    python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库。

    xlrd和xlwt这两个库用之前需要安装:cmd -> pip install xlrd  /   cmd -> pip install xlwt

    一、写Excel

      1. 手动一条条的添加数据

    import xlwt
    book = xlwt.Workbook() #新建一个excel
    sheet = book.add_sheet('sheet1') #添加一个sheet页
    
    sheet.write(0,0,'编号') #(行,列,'输入的内容') 第一行/列是0
    sheet.write(0,1,'名字')
    sheet.write(0,2,'性别')
    
    sheet.write(1,0,'1')
    sheet.write(1,1,'马春波')
    sheet.write(1,2,'')
    
    book.save('stu.xls')  #wps xls xlsx  ,微软的office 需以 .xls结尾

      运行结果,生成一个Excel,显示内容如下:

      

     2. 循环添加行、列

    import xlwt
    book = xlwt.Workbook() #新建一个excel
    sheet = book.add_sheet('sheet1') #添加一个sheet页
    
    stu_info  = [
        ['编号','姓名','密码','性别','地址'],
        [1,'machunbo','sdfsd23sdfsdf2','','北京'],
        [2,'machunbo2','sdfsd23sdfsdf2','','北京'],
        [3,'machunb3','sdfsd23sdfsdf2','','北京'],
        [4,'machunbo4','sdfsd23sdfsdf2','','北京'],
        [5,'machunbo5','sdfsd23sdfsdf2','','北京'],
        [6,'machunbo6','sdfsd23sdfsdf2','','北京'],
        [7,'machunbo6','sdfsd23sdfsdf2','','北京'],
        [8,'machunbo6','sdfsd23sdfsdf2','','北京'],
        [9,'machunbo6','sdfsd23sdfsdf2','','北京'],
        [10,'machunbo6','sdfsd23sdfsdf2','','北京'],
        [11,'machunbo6','sdfsd23sdfsdf2','','北京'],
    ]#6行5列
    
    #方法1:
    row = 0 #
    for stu in stu_info:
        sheet.write(row,0,stu[0])
        sheet.write(row,1,stu[1])
        sheet.write(row,2,stu[2])
        sheet.write(row,3,stu[3])
        sheet.write(row,4,stu[4])
        row+=1
    
    #方法2:
    row = 0 #
    for stu in stu_info:
        #stu
        col = 0  #
        # [1, 'machunbo', 'sdfsd23sdfsdf2', '男', '北京'],
        for s in stu: #控制列
            sheet.write(row,col,s) #0 3 男
            col+=1
        row+=1
    
    #方法3:
    for index,value in enumerate(stu_info):
        #  index 0
        # value ['编号','姓名','密码','性别','地址']
    
        #index 1
        #value [1,'machunbo','sdfsd23sdfsdf2','男','北京']
        for index2,v2 in enumerate(value): #enumerate同时取下标和对应的元素
            print(index,index2,v2)
            #0 1
            #1 machunbo
            #2 sdfsd23sdfsdf2
            #4 北京
            sheet.write(index,index2,v2)
    
    book.save('stu001.xls')  #wps xls xlsx  ,微软的office  xls

      运行结果,生成一个Excel,显示内容如下:

      

    二、 修改Excel

    使用前,安装xlutils模块

    import xlrd
    from xlutils import copy
    
    #编写思路如下:
    #1、先打开原来的excel
    #2、复制一份
    #3、在复制的excel上修改
    #4、保存
    
    book = xlrd.open_workbook('stu.xls')
    new_book = copy.copy(book) #复制一份原excel
    sheet = new_book.get_sheet(0) #修改excel的时候,得用get_sheet()
    sheet.write(0,0,'id') #把第一行第一列中的数据变为id
    sheet.write(0,3,'password') #第一行第四列的数据变为password
    new_book.save('stu.xls') #保存

      运行结果,修改了原Excel,显示内容如下:

    三、读取Excel

    import xlrd
    book = xlrd.open_workbook('stu3.xls')
    sheet = book.sheet_by_index(0) #查看哪个sheet的数据,根据索引指定,第一个sheet,无返回值
    sheet = book.sheet_by_name('sheet1')#查看哪个sheet的数据,根据sheet名称指定,无返回值
    print(sheet.cell(0,0).value) #获取指定单元格的内容,单元格位置cell(行、列):id
    print(sheet.cell(1,0).value) #输出1.0而不是整数1
    print(sheet.row_values(0)) #获取整行的数据,返回list:['id', '姓名', '密码', 'password', '地址']
    print(sheet.row_values(1)) #[1.0, 'machunbo', 'sdfsd23sdfsdf2', '男', '北京']
    print(sheet.col_values(0))#获取整列的数据:['id', 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0]
    print(sheet.col_values(1))
    
    print(sheet.nrows) #输出总行数:12
    print(sheet.ncols) #输出总列数:5
    for row in range(1,sheet.nrows): #从第一行开始取值,取到最后一行
        print(sheet.row_values(row)) #输出每行的数据,每行数据都是一个list

      输出结果如下:

    id
    1.0
    ['id', '姓名', '密码', 'password', '地址']
    [1.0, 'machunbo', 'sdfsd23sdfsdf2', '', '北京']
    ['id', 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0]
    ['姓名', 'machunbo', 'machunbo2', 'machunb3', 'machunbo4', 'machunbo5', 'machunbo6', 'machunbo6', 'machunbo6', 'machunbo6', 'machunbo6', 'machunbo6']
    12
    5
    [1.0, 'machunbo', 'sdfsd23sdfsdf2', '', '北京']
    [2.0, 'machunbo2', 'sdfsd23sdfsdf2', '', '北京']
    [3.0, 'machunb3', 'sdfsd23sdfsdf2', '', '北京']
    [4.0, 'machunbo4', 'sdfsd23sdfsdf2', '', '北京']
    [5.0, 'machunbo5', 'sdfsd23sdfsdf2', '', '北京']
    [6.0, 'machunbo6', 'sdfsd23sdfsdf2', '', '北京']
    [7.0, 'machunbo6', 'sdfsd23sdfsdf2', '', '北京']
    [8.0, 'machunbo6', 'sdfsd23sdfsdf2', '', '北京']
    [9.0, 'machunbo6', 'sdfsd23sdfsdf2', '', '北京']
    [10.0, 'machunbo6', 'sdfsd23sdfsdf2', '', '北京']
    [11.0, 'machunbo6', 'sdfsd23sdfsdf2', '', '北京']

     内容补充:https://www.cnblogs.com/zhoujie/p/python18.html

  • 相关阅读:
    CSU 1554 SG Value —— 思维
    最优配对问题(集合上的动态规划) —— 状压DP
    Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心
    Codeforces Round #373 (Div. 2) C. Efim and Strange Grade —— 贪心 + 字符串处理
    Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂
    Codeforces Round #374 (Div. 2) C. Journey —— DP
    Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
    Codeforces Round #374 (Div. 2) B. Passwords —— 基础题
    Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crossword —— 基础题
    UVA10129 Play on Words —— 欧拉回路
  • 原文地址:https://www.cnblogs.com/denise1108/p/10136267.html
Copyright © 2020-2023  润新知