美文网首页
2019-05-17 读写excel

2019-05-17 读写excel

作者: 年画儿 | 来源:发表于2019-05-17 18:42 被阅读0次
    #读取excel
    #1.1read
    import xlrd
    
    xlsx = xlrd.open_workbook('d:/7月下旬入库表.xlsx')
    
    table = xlsx.sheet_by_index(0)
    # 通过sheet名查找:xlsx.sheet_by_name("7月下旬入库表")
    # 通过索引查找:xlsx.sheet_by_index(3)
    print(table.cell_value(0, 0))
    
    # table.cell_value(1, 2)
    # print(table.cell(1, 2).value)
    # print(table.row(1)[2].value)
    
    for i in range(0, xlsx.nsheets):
        table = xlsx.sheet_by_index(i)
        print(table.cell_value(0, 0))
    
    # 获取所有sheet名字:xlsx.sheet_names()
    # 获取sheet数量:xlsx.nsheets
    
    for i in xlsx.sheet_names():
        table = xlsx.sheet_by_name(i)
        print(table.cell_value(3, 3))
    
    
    #写入excel
    # 1.1write
    import xlwt
    new_workbook = xlwt.Workbook()
    worksheet = new_workbook.add_sheet('new_test')
    worksheet.write(0, 0, 'test')
    new_workbook.save('d:/test.xls') 
    
    
    # 从一个表格读取 到另一个表格写入
    #1、2星
    # import xlrd,xlwt
    # xlsx = xlrd.open_workbook('d:/7月下旬入库表.xlsx')
    # new_workbook = xlwt.Workbook()
    # worksheet = new_workbook.add_sheet('new_test')
    # table = xlsx.sheet_by_index(0)
    # for i in range(0,table.nrows):
    #     for j in range(0,table.ncols):
    #         print(table.cell_value(i, j))
    #         worksheet.write(i, j, table.cell_value(i, j))
    # new_workbook.save('d:/test.xls')
    
    #3星
    import xlrd,xlsxwriter
    xlsx = xlrd.open_workbook('d:/7月下旬入库表.xlsx')
    new_workbook = xlsxwriter.Workbook('d:/test.xlsx')
    worksheet = new_workbook.add_worksheet()
    table = xlsx.sheet_by_index(0)
    for i in range(0,table.nrows):
        for j in range(0,table.ncols):
            print(table.cell_value(i, j))
            worksheet.write(i, j, table.cell_value(i, j))
    new_workbook.close()
    
    

    相关文章

      网友评论

          本文标题:2019-05-17 读写excel

          本文链接:https://www.haomeiwen.com/subject/dftaaqtx.html